3
votes

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 !

1
why are you still using RC2? I would try to help you but I think there were also changes between RC2 and 1.0. Best to migrate your project to 1.0 and then revisit any issues to get things working instead of trying to solve problems on RC2 and then may need to solve them again in 1.0 - Joe Audette
@JoeAudette ohh.. sorry my typo. I am using .net core 1.0 itself. I have updated my question - XamDev
are you implementing a custom validator yourself or just trying to use the built in Compare validator? The built in Compare validator should already work with client side validation so long as you include jquery.validate.unobtrusive.min.js in the page - Joe Audette
if you are implementing a custom validator I can show you an example how to implement IClientModelValidator - Joe Audette
@JoeAudette.. Yes please.. I will be glad ! - XamDev

1 Answers

7
votes

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