Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom error message with DataAnnotationsExtensions
    primarykey
    data
    text
    <p>I'm trying to get Scott Kirkland's DataAnnotationsExtensions to work with my MVC4 project. But I'm having problems with the client side validation of an email address. I've added a EmailAddress annotation with a error message, but when I enter an invalid email address I do not get the custom error message, but instead I get the generic email error message "Please enter a valid RecipientEmail address.".</p> <p>My class looks like this:</p> <pre><code>public class NpRequest { [DisplayName("Telefonnummer som skal overdrages")] [Required(ErrorMessage = "Angiv telefonnummeret som skal overdrages")] public string PhoneNumer { get; set; } [DisplayName("Recipient email address")] [EmailAddress(ErrorMessage = "This is my custom error message")] [Required(ErrorMessage = "The recipient email address is required")] public string RecipientEmail { get; set; } public RecipientTypeEnum RecipientType { get; set; } } </code></pre> <p>And my view:</p> <p>---SNIPPET BEGIN---</p> <pre><code> &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.PhoneNumer) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.PhoneNumer) @Html.ValidationMessageFor(model =&gt; model.PhoneNumer) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.RecipientEmail) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.RecipientEmail) @Html.ValidationMessageFor(model =&gt; model.RecipientEmail) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; </code></pre> <p>---SNIPPET END---</p> <p>EDIT: When I inspect the HTML it looks like this:</p> <pre><code>&lt;input class="text-box single-line input-validation-error" data-val="true" data-val-email="This is my custom error message" data-val-required="The recipient email address is required" id="RecipientEmail" name="RecipientEmail" type="email" value=""&gt; </code></pre> <p>It seems that my custom error message is put into the data-val-email attribute. I was under the impression that the DataAnnotationExtension automatically added my custom error message to the ModelState and thereby also adding it to the field-validation-error span, which is showing the MVC validation error.</p> <p>Is this assumption wrong? Should I write my own javascript, which extracts the custom error message attribute and injects it into the field-validation-error span?</p> <p>Can anyone see what I'm doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload