Reputation: 3647
In previous release (RC1) I have used the interface IClientModelValidator
to validate/compare the Password and Confirm Password using 'ModelClientValidationEqualToRule
' class as follows
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ClientModelValidationContext context)
{
String strMsg = AccessLocalization.GetString(_resourceID);
var modelClientValidationEqualToRule = new ModelClientValidationEqualToRule(strMsg, _OtherPropertyName);
yield return modelClientValidationEqualToRule;
}
and in jQuery
$.validator.addMethod("equalto",
function (value, element, parameters) {
return value;
});
$.validator.unobtrusive.adapters.add('equalto', [], function (options) {
options.rules.equalto = {};
options.messages['equalto'] = options.message;
});
However, now I am using .Net Core 1.0 where this interface is completely changed and it does not have 'ModelClientValidationEqualToRule
' class available.
After reading the documentation I came to know the validation will be done with the help of data-attributes. So, far I have achieved that for required field attribute, range field attribute, email address attribute etc.
But, I am not getting clear idea how can I validate Password and Confirm Password ?
Any help on this appreciated !
Upvotes: 5
Views: 8019
Reputation: 36736
IClientModelValidator has only one method to implement, an example shown here comes from a custom EnforceTrueValidator which I use to conditionally enforce checking a checkbox on a registration page if there is a required registration agreement. This method adds the needed data- attributes to wire up the client side unobtrusive validation.
public void AddValidation(ClientModelValidationContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
CheckForLocalizer(context);
var errorMessage = GetErrorMessage(context.ModelMetadata.GetDisplayName());
MergeAttribute(context.Attributes, "data-val", "true");
MergeAttribute(context.Attributes, "data-val-enforcetrue", errorMessage);
MergeAttribute(context.Attributes, "data-val-other", "#" + OtherProperty);
}
you can see the full class here, and the custom js that must also be added to the page for the custom client side validation is here
In my case the "other" property is just a boolean I use to determine if this validator should be applied because I use it in a multi tenant scenario and not all tenants may enforce a registration agreement, I set the boolean to true if the tenant has populated a registration agreement
Upvotes: 10