Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC 3 - How to make the Remote validator evaluated last
    primarykey
    data
    text
    <p>This is my model.</p> <pre><code>public class MyModel { [Display(Name = "Email Address")] [EmailAddress] // my custom validator to check valid email format [Remote("IsEmailAddressUsed", "Validation")] public string EmailAddress { get; set; } } </code></pre> <p>The problem I have is that the IsEmailAddressUsed action gets called even when I enter an invalid format for email address. The custom EmailAddress validator works fine when I remove the Remote one. I want to check against the DB only when the email address is in a correct format. Is there a way to make the Remote validator evaluated last?</p> <p>The implementation of the custom validator: EmailAddressAttribute.</p> <pre><code>public class EmailAddressAttribute : RegularExpressionAttribute, IClientValidatable { public EmailAddressAttribute() : base("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9]+(\\.[a-z0-9]+)*\\.([a-z]{2,4})$") { } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value != null) { if (!base.IsValid(value)) { return new ValidationResult( FormatErrorMessage(validationContext.DisplayName)); } } return ValidationResult.Success; } public IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { var rule = new ModelClientValidationRule { ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()) }; rule.ValidationParameters.Add("pattern", Pattern); rule.ValidationType = "emailaddress"; yield return rule; } } </code></pre> <p>And the accompanying script:</p> <pre><code>if ($.validator &amp;&amp; $.validator.unobtrusive) { $.validator.unobtrusive.adapters.addSingleVal("emailaddress", "pattern"); $.validator.addMethod("emailaddress", function (value, element, pattern) { return value.match(pattern); }); } </code></pre> <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