jquery - Custom clientside validation for required at least one in array -
jquery - Custom clientside validation for required at least one in array -
i have next custom attribute used validate if array has had value submitted:
[attributeusage(attributetargets.property, allowmultiple = true, inherited = true)] public sealed class requiredarrayattribute : requiredattribute, iclientvalidatable { public requiredarrayattribute() : base() { } public override bool isvalid(object value) { var list = (ilist)value; if (list != null && list.count > 0) { homecoming true; } homecoming false; } public ienumerable<modelclientvalidationrule> getclientvalidationrules(modelmetadata metadata, controllercontext context) { string errormessage = this.errormessage; // specific error message if set, otherwise default if (string.isnullorempty(errormessage) && metadata != null) { errormessage = formaterrormessage(metadata.getdisplayname()); } var clientvalidationrule = new modelclientvalidationrule() { errormessage = errormessage, validationtype = "requiredarray" }; homecoming new[] { clientvalidationrule }; } }
applied to
[requiredarray(errormessage = "please select @ to the lowest degree 1 product")] public ienumerable<string> productids { get; set; }
and next js (included after jquery.js, unobtrusive-ajax.js, validate.js , validate.unobtrusive.js)
(function ($) { $.validator.addmethod('requiredarray', function (value, element, params) { var selector = 'input[name=' + $(element).attr('name') + ']:checked'; homecoming $(selector).length > 0; }, 'clientside should not postback'); $.validator.unobtrusive.adapters.addbool('requiredarray'); })(jquery);
i have tried changing addmethod
return false;
still nil (and no errors in console)
i wondering if spot had done wrong above code. have done similar thing address attribute , works fine reason can't 1 work?
further server side validation works.
razor code validation message
@html.validationmessagefor(m => m.productids)
example html rendered checkboxes:
<input type="checkbox" class="checkbox" name="productids" value="exampleid1234" id="checkbox-1" checked="checked" /> <input type="checkbox" class="checkbox" name="productids" value="exampleid1235" id="checkbox-2" checked="checked" />
edit
further can clientside validation work using following
$.validator.addclassrules('required-group', { 'requiredarray': true });
instead of validator.unobtrusive.adapters.addbool
method in above. have added class required-group
checkbox
however, if utilize doesn't add together proper mvc error message, clientside should not postback
set in above code. have tried adding next attributes checkbox:
data-val="true" data-val-required="please select @ to the lowest degree 1 sample"
and tried using data-val-requiredarray
makes no difference know how alter above clientside rules create utilize mvc error message
so fixed next changes html:
<input type="checkbox" class="checkbox required-group" name="productids" value="exampleid1234" id="checkbox-1" checked="checked" data-val-required="please select @ to the lowest degree 1 sample" /> <input type="checkbox" class="checkbox required-group" name="productids" value="exampleid1235" id="checkbox-2" checked="checked" />
data-val-required
needs on first checkbox in array. using next jquery:
(function ($) { $.validator.addmethod('requiredarray', function (value, element, params) { homecoming $('input[name=' + $(element).attr('name') + ']:checked').length > 0; }, 'please select @ to the lowest degree one'); $.validator.addclassrules('required-group', { 'requiredarray': true }); var errormessage = $('.required-group').eq(0).data('val-requiredarray'); if (errormessage && errormessage !== "") { $.validator.messages.requiredarray = errormessage; } })(jquery);
i have changed addbool
addclassrules
, used $.validator.messages
set error message.
jquery asp.net-mvc-4 jquery-validate unobtrusive-validation
Comments
Post a Comment