Asp.net mvc unobtrusive validation showing validation error message on render

Short version: why is the validation message visible by default when data-valmsg-replace='false'

? and how to fix it?

Details:

In my razor, I do:

@Html.TextArea("Description", Model.FormData.Description, new { @class="span12" })
@Html.ValidationMessage("Description", L("Description is required"))

      

the validation message is displayed as (as expected):

<span class="field-validation-valid" data-valmsg-for="Description" data-valmsg-replace="false">Description is required</span>

      

and the message is displayed on the render page. Seems to be by design, but doesn't make sense (notice data-valmsg-replace='false'

).

Questions

  • What is the rationale behind this design decision, specifically so that the error message is visible by default when it comes with data-valmsg-replace='false'

    ?

  • Is there a better solution than adding .field-validation-valid { display: none; }

+3


source to share


2 answers


I had the same problem, it ended up by default in Global.asax:

Decision



<add key="ClientValidationEnabled" value="false" />
<add key="UnobtrusiveJavaScriptEnabled" value="false" />

      

0


source


the following message may contain speculation

ASP.NET MVC project creation will be done with this rule:

.field-validation-valid {
  display: none;
}

      

which means the container of validation errors will not show up OOB (in my case, I was unable to transfer this rule to my own .css site)



Why not an unobtrusive check of the .hide()

container if there are no errors?
Let's say I want a green checkbox in my error container if there are no errors. If unobtrusive. Validation will hide the container, then I couldn't do that.

So my answers:

  • unobtrusive should be unobtrusive, so it doesn't hide the container by default. You want it hidden - you customize it the way you want it.
  • .field-validation-valid { display: none; }

    is the smartest and best solution I can think of.
0


source







All Articles