How to use unobtrusive JS validation with custom validation attributes in MVC3?

I have this custom validation attribute called AsteriskRequiredAttribute, which comes from the attribute Required, and its function is only to display an asterisk (*) when the text field field does not matter.

MVC3's unobtrusive JS check seems to work just fine from the nose with an attribute Required, but not with my custom attribute - nothing happens.

What's happening?

+3
source share
1 answer

http://samipoimala.com/it/2010/11/29/unobtrusive-client-validation-in-asp-net-mvc-3/

, - . , System.ComponentModel.DataAnnotations.ValidationAttribute System.Web.Mvc.IClientValidatable. .

1) public bool IsValid ( ) , (, javascript). , , .

2) , ModelClientValidationRule. , , . , :

public class ModelClientValidationEmailRule : ModelClientValidationRule
{
    public ModelClientValidationEmailRule(string errorMessage)
    {
        base.ErrorMessage = errorMessage;
        base.ValidationType = "email";
    }
}

3) IEnumerable GetClientValidationRules ( ModelMetadata, ControllerContext)

, , :

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
    yield return new ModelClientValidationEmailRule(FormatErrorMessage(metadata.GetDisplayName()));
}

, , , , jQuery Validate.

+3

Source: https://habr.com/ru/post/1795897/


All Articles