Custom filed validation in CakePHP

旧城冷巷雨未停 提交于 2019-12-11 05:21:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!