Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC 2 RC - client side validation using Data Annotations fails with multiple attributes
    primarykey
    data
    text
    <p>I'm having trouble with the JavaScript being emitted for client side validation when using Data Annotations on a model. Here's a sample of a model that works just fine with two required fields followed by the JavaScript that's emitted:</p> <pre><code>public class LoginUserViewModel { [Required(ErrorMessage = "Email required")] public string EmailAddress { get; set; } [Required(ErrorMessage="Password required")] public string Password { get; set; } } } //&lt;![CDATA[ if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; } window.mvcClientValidationMetadata.push({"Fields":[{"FieldName":"EmailAddress","ReplaceValidationMessageContents":true,"ValidationMessageId":"form0_EmailAddress_validationMessage","ValidationRules":[{"ErrorMessage":"Email required","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"Password","ReplaceValidationMessageContents":true,"ValidationMessageId":"form0_Password_validationMessage","ValidationRules":[{"ErrorMessage":"Password required","ValidationParameters":{},"ValidationType":"required"}]}],"FormId":"form0","ReplaceValidationSummary":false}); //]]&gt; </code></pre> <p>as soon as I put another attribute on the EmailAddress field, a regular expression attribute, the JavaScript no longer has any rules emitted. Notice in the JavaScript below there is just an empty array where the rules should be. Here's the change and the script. Any ideas?</p> <pre><code>public class LoginUserViewModel { [Required(ErrorMessage = "Email required")] [RegularExpression(@"^[a-z0-9]+([-+\.]*[a-z0-9]+)*@[a-z0-9]+([-\.][a-z0-9]+)*{2,4}$", ErrorMessage = "Invalid email format")] public string EmailAddress { get; set; } [Required(ErrorMessage="Password required")] public string Password { get; set; } } //&lt;![CDATA[ if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; } window.mvcClientValidationMetadata.push({"Fields":[{"FieldName":"EmailAddress","ReplaceValidationMessageContents":true,"ValidationMessageId":"form0_EmailAddress_validationMessage","ValidationRules":[]},{"FieldName":"Password","ReplaceValidationMessageContents":true,"ValidationMessageId":"form0_Password_validationMessage","ValidationRules":[{"ErrorMessage":"Password is required.","ValidationParameters":{},"ValidationType":"required"}]}],"FormId":"form0","ReplaceValidationSummary":false}); //]]&gt; </code></pre> <p>Any ideas what's causing the rules to disappear when the new attribute is added?</p> <p>Thanks!</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.
    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