jquery - FluentValidation: Get CustomState on client side -
i want integrate bootstraps .alert alert-warning
classes fluentvalidation
show/hide warning messages. thought put custom state on rule , send client side show , hide alert message, cannot figure out. here have far:
public class foovalidator : abstractvalidator<foovm> { public foovalidator () { rulefor(model => model.temperature) .notnull() .lessthanorequalto(model => model.maxtemp) .withstate(model => validationtype.warning) .withmessage("warning, temp should less or equal {comparisonvalue}") .greaterthanorequalto(model => model.mintemp) .withstate(model => validationtype.warning) .withmessage("warning, temp should greater or equal {comparisonvalue}"); } }
and here lessthanorequalto validator client rules. please note have todo @ end set state argument client
public class lessthanorequalpropertyvalidator : fluentvalidationpropertyvalidator { public lessthanorequalpropertyvalidator(modelmetadata metadata, controllercontext controllercontext, propertyrule rule, ipropertyvalidator validator) : base(metadata, controllercontext, rule, validator) { } public override ienumerable<modelclientvalidationrule> getclientvalidationrules() { if (!shouldgenerateclientsiderules()) yield break; var validator = (lessthanorequalvalidator)validator; object val; string variableinmsg; var propertytocompare = validator.membertocompare propertyinfo; // value property instead of using property if (propertytocompare != null) { val = this.metadata.container.gettype().getproperty(propertytocompare.name).getvalue(this.metadata.container, null); variableinmsg = "comparisonvalue"; } else { val = validator.valuetocompare; variableinmsg = "valuetocompare"; } var errormessage = new messageformatter() .appendpropertyname(rule.getdisplayname()) .appendargument(variableinmsg, val) .buildmessage(validator.errormessagesource.getstring()); var rule = new modelclientvalidationrule(); rule.errormessage = errormessage; rule.validationtype = "lessthanorequal"; rule.validationparameters["valuetocompare"] = val; rule.validationparameters["state"] = // todo: how can send state across, know can view when validating obj yield return rule; } }
and here client code want able pick state in params object. warnings return true;
$.validator.addmethod("lessthanorequal", function (value, element, params) { var isvalid = this.optional(element) || parsefloat(value) <= parsefloat(params.valuetocompare) // todo: how state //if(params.state === "warning") { // if (isvalid) { // $(".alert-warning").hide(); // } // else { // $(".alert-warning").show(); // } // return true; //} return isvalid }); $.validator.unobtrusive.adapters.add("lessthanorequal", ["valuetocompare"], function (options) { options.rules['lessthanorequal'] = options.params; if (options.message) { options.messages['lessthanorequal'] = options.message; } });
kudos goes @kundansinghchouhan pointing out should have seen. state object in data-val
attribute, not value use directly on client. below did work in lessthanorequalpropertyvalidator
rule.validationparameters["state"] = this.customstateprovider.invoke(null);
i not know ramifications have when validationerror
list posted server because class stores state
value, review if ever need use class.