问题
i need help how to develop validation rule for next case...
my cakephp web form contains several dynamically generated checkboxes. end user must select at least one of them.
none of those checkboxes are fields in db table.
can you help me how to create validation rule for that?
tried to do by adding in model something like
var $validate = array(
'topic' => array(
'rule' => 'myRule',
'message' => 'Check on at least one checkbox'
)
)
function myRule() {
//just give me false to know it works...
return false;
}
also, for that case, how to place error message, below those checkboxes?
tnx in adv!
回答1:
With Custom Validation rules in CakePHP, if you are calling a function instead of a RegEx it must be the first parameter in an associative array, so it would look something like this instead.
var $validate = array(
'topic' => array(
'rule' => array('myRule'),
'message' => 'Check on at least one checkbox'
)
)
function myRule() {
//just give me false to know it works...
return false;
}
Additionally if you wanted to include function arguments you would put them as extra parameters in the rule array.
var $validate = array(
'topic' => array(
'rule' => array('myRule', 10),
'message' => 'Check on at least one checkbox'
)
)
function myRule($id) {
if (array_shift($id) = 10) {
return false;
}
}
http://book.cakephp.org/view/152/Adding-your-own-Validation-Methods
来源:https://stackoverflow.com/questions/4845212/custom-filed-validation-in-cakephp