Пример #1
0
        public static IHtmlContent TextFormGroupFor <TModel, TValue>(this IHtmlHelper <TModel> html,
                                                                     Expression <Func <TModel, TValue> > expression, IUkOnly UkOnlyModel = null)
        {
            var control = html.TextBoxFor(expression, new { @class = "form-control" });

            return(BuildFormGroupForControl(html, expression, control, UkOnlyModel));
        }
Пример #2
0
        private static IHtmlContent BuildFormGroupForControl <TModel, TValue>(IHtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, IHtmlContent control, IUkOnly UkOnlyModel = null)
        {
            var fieldName =
                html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
            var hasErrors = html.ViewData.ModelState[fieldName]?.Errors != null &&
                            html.ViewData.ModelState[fieldName].Errors.Any();

            return(new HtmlContentBuilder()
                   .AppendHtml(hasErrors
                    ? "<div class='form-group form-group-error'>"
                    : "<div class='form-group'>")
                   .AppendHtml("<fieldset>")
                   .AppendHtml($"<legend id='{GenerateLegendId(html, expression)}'>")
                   .AppendHtml(html.LabelWithHintFor(expression, UkOnlyModel))
                   .AppendHtml("<span class=\'error-message\'>")
                   .AppendHtml(html.ValidationMessageFor(expression))
                   .AppendHtml("</span>")
                   .AppendHtml("</legend>")
                   .AppendHtml(control)
                   .AppendHtml("</fieldset>")
                   .AppendHtml("</div>"));

            //var legend = new TagBuilder("legend");
            //legend.Attributes.Add("id", GenerateLegendId(html, expression));

            //var error = new TagBuilder("span");
            //error.AddCssClass("error-message");

            //error.InnerHtml.Append(html.ValidationMessageFor(expression).ToString());
            //legend.InnerHtml.Append($"{html.LabelWithHintFor(expression)} {error}");

            //var fieldset = new TagBuilder("fieldset");
            //fieldset.InnerHtml.Append($"{legend} {control}");

            //var parent = new TagBuilder("div");
            //parent.AddCssClass("form-group");
            //parent.InnerHtml.Append(fieldset.ToString());
            //if (hasErrors)
            //{
            //    parent.AddCssClass("form-group-error");
            //}

            //return new HtmlString(parent.ToString());
        }
Пример #3
0
        private static bool IsRequiredIfUk <TModel, TValue>(Expression <Func <TModel, TValue> > expression, IUkOnly model)
        {
            var memberExpression = expression.Body as MemberExpression;

            if (memberExpression == null)
            {
                throw new InvalidOperationException("Expression must be a member expression");
            }

            return(memberExpression.Member.GetCustomAttribute <RequiredIfUkAddressAttribute>() != null && model.IsUk);
        }
Пример #4
0
        public static IHtmlContent LabelWithHintFor <TModel, TValue>(this IHtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, IUkOnly ukOnlyModel = null)
        {
            var htmlFieldName     = ExpressionHelper.GetExpressionText(expression);
            var metadata          = ExpressionMetadataProvider.FromLambdaExpression(expression, html.ViewData, html.MetadataProvider).Metadata;
            var resolvedLabelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
            var optionalText      = metadata.IsRequired ||
                                    IsCompared(expression) ||
                                    IsHiddenOptional(expression) ||
                                    IsDateRequired(expression) ||
                                    IsRequiredIf(expression) ||
                                    IsAssertThat(expression) ||
                                    IsRequireTrue(expression) ||
                                    IsRequiredForShellfish(expression, html.ViewData.Model as IShellfishSection) ||
                                    IsRequiredIfUk(expression, ukOnlyModel ?? html.ViewData.Model as IUkOnly) ? string.Empty : "(optional)";

            return(new HtmlContentBuilder()
                   .AppendHtml(
                       $"<label class='form-label'>{resolvedLabelText} {optionalText} " +
                       $"<span class='form-hint'>{metadata.Description}</span></label>"));
        }