依赖字段上的Cakephp自定义验证

时间:2011-12-26 11:58:53

标签: php validation cakephp cakephp-1.3

我需要在我的模型中进行自定义验证,其中字段一不能复制,字段二将区分它,如复合键。就像下面一样。

    public $validate = array(
    'field1' => array(
        'required' => array(
            'rule' => 'notEmpty',
            'message' => 'Can\'t left blank.'
        ),
        'unique' => array(
            'rule' => array('validateNumberType'),
            'message' => 'Already exist'
        ),
        )
    );
    function validationRule() {
    $validation_var = $this->find('first', array('conditions' => array('Model.field1' => 'field1', 'Model.field2' => 'field2')));
    return $validation_var;
}

我尝试了但是没有用,可能是函数中的错误吗?

3 个答案:

答案 0 :(得分:0)

将您的功能名称更改为 validateNumberType ,因为您已在规则中对其进行了定义,例如:

public $validate = array(
    'field1' => array(
        'required' => array(
            'rule' => 'notEmpty',
            'message' => 'Can\'t left blank.'
        ),
        'unique' => array(
            'rule' => array('validateNumberType'),
            'message' => 'Already exist'
        ),
        )
    );
    function validateNumberType() {
    $validation_var = $this->find('first', array('conditions' => array('Model.field1' => 'field1', 'Model.field2' => 'field2')));
    return $validation_var;
}

请参见此处:Validation for Dependent Type

答案 1 :(得分:0)

var $validate = array(
'slug' => array(
'rule' => 'validationRule',
'message' => 'error message'
)
);
function validationRule($check) {
//validation
//$check is value of the field
}

答案 2 :(得分:0)

var $validate = array(
   'slug' => array(
        'rule' => 'validationRule',
        'message' => 'error message'
    )
);
function validationRule($check) {
    //validation
    //$check is value of the field
    //$this->data is value of whole submited form

    // return true if validate, false either
}