c# - Validating two checkboxes -


i have custom validation code checking 2 checkboxes checked displaying 1 message both of them.

public class severalcheckboxesrequiredattribute : validationattribute, iclientvalidatable {     private readonly string[] _properties;      public severalcheckboxesrequiredattribute(params string[] properties)     {         errormessage = "you must confirm have entered correct information ";         errormessage += "for income , expenditure. wrong information may result in ";         errormessage += "us not being able lend @ time or in future.";          _properties = properties;     }      protected override validationresult isvalid(object value, validationcontext validationcontext)     {         if (_properties == null || _properties.length < 1)         {             return null;         }          foreach (var prop in _properties)         {             var property = validationcontext.objecttype.getproperty(prop);             var propertyvalue = (bool)property.getvalue(validationcontext.objectinstance, null);             if (!propertyvalue)             {                 // @ least 1 property false  => model not valid                 return new validationresult(formaterrormessage(validationcontext.displayname));             }         }          return null;     }      public ienumerable<modelclientvalidationrule> getclientvalidationrules(modelmetadata metadata, controllercontext context)     {         var rule = new modelclientvalidationrule         {             errormessage = errormessage,             validationtype = "severalcheckboxesrequired"         };          rule.validationparameters["properties"] = string.join(",", _properties);          yield return rule;     } } 

and client side is:

jquery.validator.addmethod('severalcheckboxesrequired', function (value, element, params) {     var props = params.properties.split(','),         isvalid = true;     (var = 0; < props.length; i++) {         var property = props[i];         if (!$('#' + property).is(':checked')) {             isvalid = false;             break;         }     }     return isvalid; }, '');  jquery.validator.unobtrusive.adapters.add(     'severalcheckboxesrequired',     ['properties'],     function (options) {         options.rules['severalcheckboxesrequired'] = options.params;         options.messages['severalcheckboxesrequired'] = options.message;     } ); 

the model properties related be:

[severalcheckboxesrequired("accurateinformation", "futureinformation")] [display(name = "please tick here confirm have provided accurate information.")] public bool accurateinformation { get; set; }  [display(name = "please tick here confirm have considered potential future information.")] public bool futureinformation { get; set; } 

and view contains this

<div class="grid_12">     <div class="checkboxwrapper">         @html.labelfor(m => m.accurateinformation)         @html.checkboxfor(m => m.accurateinformation)         @html.labelfor(m => m.futureinformation)         @html.checkboxfor(m => m.futureinformation)     </div> </div> <div class="grid_12">         @html.validationmessagefor(m => m.accurateinformation) </div> 

my aim highlight them whenever not checked not working. that's minor concern now. main problem achieving decorate 1 of them if use attribute @ class level i'm not able have client side validation.

and causes , issue in kind of scenario: submit form first 1 (the decorated one) checked , not second 1 , client side error (not highlighted though message). check second 1 , validator method not trigger , have hit submit button make message disappear. there way of binding second 1 same validator method? there better solution this? possible highlight fawled checkbox?

thanks

update: in second stint on issue came far ideal workaround, in case run same problem , don't have better...

i add class 'shared' 2 checkboxes (notice have use html.checkboxfor , not editorfor in order add class)

<div class="grid_12">     <div class="checkboxwrapper">         @html.labelfor(m => m.accurateinformation)         @html.checkboxfor(m => m.accurateinformation, new {@class='shared'})         @html.labelfor(m => m.futureinformation, new {@class='shared'})         @html.checkboxfor(m => m.futureinformation)     </div> </div> <div class="grid_12"> 

and code in js file

$(document).ready(function(){     $('.shared').on('change', function () {         $('.shared[data-val-severalcheckboxesrequired]').valid();     }); }); 

which triggers validation on annotated checkbox every time of dependants (or itself) changes, custom validator check of them checked every time trigger happened of them have checked in order make message disappear.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -