Client side validation for custom validator asp.net MVC 4

Snavebelac

I have a custom server side validator that works but I am struggling to get the client side working. Here is what I have so far.

The Validator is checking that one value entered is at least a percentage of another value entered.

public sealed class PercentageOfAttribute : ValidationAttribute, IClientValidatable
{
    private readonly string sourcePropertyName;
    private readonly int minimumPercentage;

    public PercentageOfAttribute(string sourcePropertyName, int minimumPercentage)
    {
        this.sourcePropertyName = sourcePropertyName;
        this.minimumPercentage = minimumPercentage;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var propertyName = validationContext.ObjectType.GetProperty(this.sourcePropertyName);
        if (propertyName == null)
            return new ValidationResult(String.Format("Uknown property {0}", this.sourcePropertyName));

        int propertyValue = (int)propertyName.GetValue(validationContext.ObjectInstance, null);

        var minValue = (propertyValue / 100) * this.minimumPercentage;
        if ((int)value > minValue)
            return ValidationResult.Success;

        return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));

    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule
        {
            ErrorMessage = this.ErrorMessageString,
            ValidationType = "percentageof"
        };
        rule.ValidationParameters["propertyname"] = this.sourcePropertyName;
        rule.ValidationParameters["minimumpercentage"] = this.minimumPercentage;
        yield return rule;
    }
}

And the javascript for the client side...

jQuery.validator.addMethod("percentageof", function (value, element, params) {
    var propertyname = params['propertyname'];
    var minimumpercentage = param['minimumpercentage'];
    var propValue = $('#' + propertyname).val();
    var minValue = (propValue / 100) * minimumpercentage;
    if (value >= minValue) {
        return true;
    }
    return false;
});
jQuery.validator.unobtrusive.adapters.add("percentageof", ["propertyname", "minimumpercentage"], function (options) {
    options.rules['propertyname'] = options.params.propertyname;
    options.rules['minimumpercentage'] = options.params.minimumpercentage;
    options.message['percentageof'] = options.message;
});

I have debugged the addapters.add function and this is passing in the correct data. The "data-val-percentageof-*" values appear in the html and have the correct values.

The javascript error I am currently getting is "$.validator.methods[method] is undefined". I think this means that I have not correctly set up the client side validator. I've read through countless examples and tutorials but can't seem to figure out a combination that works.

If I remove all my custom stuff then the default client side validation works perfectly.

Can any one help show me what I am doing wrong?

EDIT:

The rendered html is as follows

<div class="form-group">
<label class="col-sm-3 control-label" for="PurchasePrice">Purchase Price (£36,000 minimum) *</label>
<div class="col-sm-6">
    <input id="PurchasePrice" class="form-control valid" type="text" value="0" name="PurchasePrice" data-val-required="Please enter a Purchase Price" data-val-range-min="36000" data-val-range-max="2147483647" data-val-range="Please enter at least £36,000" data-val-number="The field Purchase Price (£36,000 minimum) * must be a number." data-val="true">
    <span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="PurchasePrice"></span>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="Deposit">Available Deposit (30% minimum)*</label>
<div class="col-sm-6">
    <input id="Deposit" class="form-control" type="text" value="0" name="Deposit" data-val-required="Please enter an Available Deposit" data-val-percentageof-propertyname="PurchasePrice" data-val-percentageof-minimumpercentage="30" data-val-percentageof="Please enter value that is at least 30% of the purchase price" data-val-number="The field Available Deposit (30% minimum)* must be a number." data-val="true">
    <span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="Deposit"></span>
</div>
</div>
Snavebelac

I managed to get this working by changing the format of the ...adapters.add function as follows

jQuery.validator.addMethod("percentageof", function (value, element, params) {
  var propertyname = params['propertyname'];
  var minimumpercentage = params['minimumpercentage'];
  var propValue = $('#' + propertyname).val();
  var minValue = (propValue / 100) * minimumpercentage;
  if (value >= minValue) {
    return true;
  }
  return false;
});
jQuery.validator.unobtrusive.adapters.add("percentageof", ["propertyname", "minimumpercentage"], function (options) {
  options.rules["percentageof"] = options.params;
  if (options.message) {
    options.messages['percentageof'] = options.message;
  }
});

I was incorrectly trying to add the paramaters as rules!! There was also a typo in the addMethod section of code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Asp.net MVC 4 Client Side Validation Not Work

asp mvc core 3 Client side validation for a custom attribute validation

ASP.Net Core MVC - Client-side validation for custom attribute

How to add required field validation on client side with custom message for Radio Button in ASP.Net MVC?

Issue with jQuery datepicker and client side validation for dates in ASP.net MVC 4

Adding client side validation failures to ASP.NET MVC ModelState

Configure culture for client side validation in ASP.NET Core MVC

Conditional client side validation in ASP.NET MVC Core 3.1

Client side validation in asp.net mvc3

Custom Client-Side Validation not triggering in ASP.NET Core

MVC Custom Validation String Array Client Side

ASP.NET MVC password validator doesn't warn about some mandatory requirements on client side?

How to make RequiredIf Client-side and server-side validation in ASP.NET Core 3.1 MVC?

Client Side Validation not performing MVC 4

MVC 4 client side validation to be shown in a dialog

Custom validation summary in ASP.NET MVC 4

ASP.NET MVC Custom Validator not triggering

How to use Globalize.js to configure culture for client side validation in ASP.NET Core MVC

ASP.NET MVC ValidationAttribute causes invalid client side validation rules

How to stop ajax spinner on ASP.NET MVC register form if client side validation fails

Client side validation "required" with conditional logic to kendo DropDownList from the ASP.NET MVC UI Api

How to do client side validation of listbox in asp.net mvc application

How to trigger ASP .NET Core MVC client-side validation without submitting

ASP.NET required validator change from client side

ASP.NET Core Create a Custom Data Annotation for JSON validation also from client side

Custom client side validation attribute with parameter in ASP.NET Core using IClientModelValidator

How to set up Client side validation for a custom RequiredIf attribute for Asp.NET Core 3.0

ASP.NET Core 6 : custom vallidation attribute with client-side validation

ASP validation client side