According to this post and this one I tried to implement custom validation for greater than logic.
So I have the following code :
public class DynamicRangeGreaterThenValidator : ValidationAttribute, IClientValidatable
{
private readonly string _minPropertyName;
public DynamicRangeGreaterThenValidator(string minPropertyName)
{
_minPropertyName = minPropertyName;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var minProperty = validationContext.ObjectType.GetProperty(_minPropertyName);
if (minProperty == null)
{
return new ValidationResult(string.Format("Unknown property {0}", _minPropertyName));
}
var minValue = (double)minProperty.GetValue(validationContext.ObjectInstance, null);
var currentValue = (double)value;
if (currentValue <= minValue)
{
return new ValidationResult(
string.Format(
ErrorMessage,
minValue
)
);
}
return null;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata,
ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ValidationType = "greaterthen",
ErrorMessage = this.ErrorMessage,
};
rule.ValidationParameters["minvalueproperty"] = _minPropertyName;
yield return rule;
}
}
JavaScript:
jQuery(document).ready(function () {
jQuery.validator.unobtrusive.adapters.add(
'greaterthen',
['minvalueproperty'],
function(options) {
// simply pass the options.params here
options.rules['greaterthen'] = options.params;
options.messages['greaterthen'] = options.message;
}
);
jQuery.validator.addMethod('greaterthen', function(value, element, params) {
var minValue = parseFloat(jQuery('input[name="' + params.minvalueproperty + '"]').val(), 10);
var currentValue = parseInt(value, 10);
if (isNaN(minValue) || isNaN(currentValue) || minValue >= currentValue) {
var message = jQuery(element).attr('data-val-greaterthen');
jQuery.validator.messages.greaterthen = jQuery.format(message, minValue);
return false;
}
return true;
}, '');
});
My model :
public class TestModel
{
[Required(ErrorMessageResourceType = typeof (Resource), ErrorMessageResourceName = "RequiredParameter")]
/* [Display(Name = "MinQuantity", ResourceType = typeof (Resource))]*/
public double MinQuantity { get; set; }
[Required(ErrorMessageResourceType = typeof (Resource), ErrorMessageResourceName = "RequiredParameter")]
public double MaxQuantity { get; set; }
[Required(ErrorMessageResourceType = typeof (Resource), ErrorMessageResourceName = "RequiredParameter")]
[Display(Name = "NormalQuantity", ResourceType = typeof (Resource))]
[DynamicRangeGreaterThenValidator("MinQuantity", ErrorMessage = "Value must be greater than {0}")]
public double NormalQuantity { get; set; }
}
Unfortunately this code is working just for server side and I can't make this code to work for client side.
My problem is that I didn't find any rules for creating client side validation. I don't now how should be the name of adapter and method to get it work. Please help me with this code and if is possible to explain how client side validation should look like to work.
Thanks.