EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: chrwei on August 22, 2013, 02:09:36 PM



Title: validatebox, adding a new rule
Post by: chrwei on August 22, 2013, 02:09:36 PM
I'm looking for a cleaner way to do this:
Code:
<script>
$.extend($.fn.validatebox.defaults.rules, {
pickonemore: {
validator: function(value,param){
var hasone = false;
$(param).each(function(k,p) {
if ($(p).val() != "") hasone = true;
});
if(hasone) {
$(param).each(function(k,p) {
$(p).validatebox('disableValidation');
});
} else {
$(param).each(function(k,p) {
$(p).validatebox('enableValidation');
});
}
return hasone;
},
message: 'Enter at least one value in this group.'
}
});
</script>
<input id="box1" class="easyui-validatebox" type="text" name="box1" required="required" validType="pickonemore['#box1','#box2','#box3']"></input></td>
<input id="box2" class="easyui-validatebox" type="text" name="box2" required="required" validType="pickonemore['#box1','#box2','#box3']"></input></td>
<input id="box3" class="easyui-validatebox" type="text" name="box3" required="required" validType="pickonemore['#box1','#box2','#box3']"></input></td>

goal is to require one or more of the 3 values to be entered.  I'm using enableValidation/disableValidation calls because my new validator does not seemed to be called when a box is blank.  is there a better way?


Title: Re: validatebox, adding a new rule
Post by: stworthy on August 22, 2013, 07:06:54 PM
After calling 'disableValidation' method, you need to call 'enableValidation' method again in some conditions. The simplest way to enable validation again is to recall 'enableValidation' method when value changed.
Code:
$('#box1,#box2,#box3').bind('keyup',function(){
$('#box1,#box2,#box3').validatebox('enableValidation').validatebox('validate');
});


Title: Re: validatebox, adding a new rule
Post by: chrwei on August 23, 2013, 06:27:11 AM
this is because if the value is selected and deleted, the validation won't fire since it doesn't run when the value is blank?  would it be a bad thing to run validation functions on blank values anyway?