Use client-side validation when using the special attribute [required]

in this project, we do not use the default dataannotation attributes from the System.ComponentModel.DataAnnotations namespace, but custom attributes are created.

So, we put the attribute [required] in the property, but this is a custom user.

For server-side validation, we were able to override the validation using a specialized verification provider, but we adhere to client-side validation.

As I read in the docs, I see that when you use the [required] attribute by default, these attributes are displayed in html elements:

 data-val-lengthmax="10" data-val-length-min="3" data-val-required="The ClientName field is required." 

I assume this is done by a framework that reads the normal required attribute and then displays the html attributes.

Can we make a framework for these attributes for us too?

+4
source share
2 answers

Can we make a framework for these attributes for us too?

Yes, there are 2 possibilities:

  • Add your custom attribute to the IClientValidatable interface, where you will implement client validation rules.
  • Register your custom DataAnnotationsModelValidator<TAttribute> where TAttribute will be your custom validation attribute and where you will implement your own client-side validation rules (the approach Microsoft uses to implement client-side validation for the Required attribute, and therefore, if you write your own validation attribute, which arises from it, you do not receive verification on the client side). Then you need to register your own model validator with a custom attribute by calling DataAnnotationsModelValidatorProvider.RegisterAdapter .
+4
source

To enable client validation in a user attribute, you can implement the IClientValidatable interface in your attribute:

 public class requiredAttribute : ValidationAttribute, IClientValidatable { ... public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { return new[] { new ModelClientValidationRule { ErrorMessage = "<Your error message>", ValidationType = "required" } }; } } 

Alternatively, you can implement a validation adapter for your attribute:

 public class requiredAttributeAdapter : DataAnnotationsModelValidator<requiredAttribute> { public requiredAttributeAdapter(ModelMetadata metadata, ControllerContext context, RequiredAttribute attribute) : base(metadata, context, attribute) { } public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { return new[] { new ModelClientValidationRule { ErrorMessage = "<Your error message>", ValidationType = "required" } }; } } 

And register it using the data annotation check mechanism in the Global.asax file:

 protected void Application_Start() { ... DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(requiredAttribute), typeof(requiredAttributeAdapter)); } 

Of course, you need to make sure that you refer to your attribute in the above classes.

+2
source

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


All Articles