コード例 #1
0
        protected void RenderValidation(TagHelperContext context, TagHelperOutput output)
        {
            if (AspFor != null)
            {
                if (IsNeedValidation.GetValueOrDefault())
                {
                    foreach (TagHelperAttribute attr in MvcTagHelperOutput.Attributes)
                    {
                        if (attr.Name.StartsWith("data-"))
                        {
                            output.MergeAttribute(attr.Name, attr.Value);
                        }
                    }
                }

                if (IsNeedValidationMessage.GetValueOrDefault())
                {
                    //var builder = HtmlGenerator.GenerateValidationMessage(ViewContext, AspFor.ModelExplorer, AspFor.Name, null, "", new { @class = "help-block" });

                    var builder = new TagBuilder("div")
                    {
                        TagRenderMode = TagRenderMode.Normal
                    };
                    builder.AddCssClass("help-block");
                    builder.AddCssClass("invalid-feedback");
                    builder.MergeAttribute("data-valmsg-for", AspFor.Name);
                    builder.MergeAttribute("data-valmsg-replace", "true");

                    // Error Message
                    var modelState = ViewContext.ViewData.ModelState.FirstOrDefault(k => k.Key == AspFor.Metadata.PropertyName).Value;
                    if (modelState != null && modelState.ValidationState == ModelValidationState.Invalid)
                    {
                        builder.AddCssClass("field-validation-error");
                        builder.InnerHtml.Append(modelState.Errors.FirstOrDefault()?.ErrorMessage);
                    }
                    else
                    {
                        builder.AddCssClass("field-validation-valid");
                    }

                    output.PostElement.AppendHtml(builder);
                }
            }
        }
コード例 #2
0
        protected void PrepareForInputGroup(TagHelperContext context, TagHelperOutput output)
        {
            if (InputGroupContext != null)
            {
                if (LabelText.IsNotNullOrEmpty())
                {
                    InputGroupContext.LabelText = LabelText;
                    LabelText = null;
                }

                if (HelpText.IsNotNullOrEmpty())
                {
                    InputGroupContext.HelpText = HelpText;
                    HelpText = null;
                }

                if (AspFor != null)
                {
                    if (IsNeedValidation.GetValueOrDefault() || IsNeedValidationMessage.GetValueOrDefault())
                    {
                        InputGroupContext.AspFor             = AspFor;
                        InputGroupContext.MvcTagHelperOutput = MvcTagHelperOutput;

                        InputGroupContext.IsNeedValidation        = IsNeedValidation;
                        InputGroupContext.IsNeedValidationMessage = IsNeedValidationMessage;

                        IsNeedValidationMessage = false;
                    }
                }

                if (IsFormHorizontal)
                {
                    InputGroupContext.IsFormHorizontal = IsFormHorizontal;
                    IsFormHorizontal = false;
                }
            }
        }