Cakephp验证错误

时间:2016-03-16 12:10:56

标签: validation cakephp

控制器代码


    if(!empty($this->data)){
        if($this->{$this->modelClass}->signupValidate()){
           $this->{$this->modelClass}->save($this->data);
        }
    }

型号代码


    function signupValidate(){      
            $validate1  =   array(
                'first_name' => array(
                    'rule1' => array(
                        'rule' => 'notEmpty',
                        'message' => __('Please enter first name',true)
                    )
                )
            );
    $this->validate     =   $validate1;     
    return $this->validates();
    }

验证无法正常工作

2 个答案:

答案 0 :(得分:0)

你应该在你的控制器中设置


    $this->{$this->modelClass}->set($this->data);

喜欢这个



    if(!empty($this->data)){
        $this->{$this->modelClass}->set($this->data);
        if($this->{$this->modelClass}->signupValidate()){
           $this->{$this->modelClass}->save($this->data);
        }
    }

答案 1 :(得分:0)

您的模型会在保存数据之前自动调用验证,否则您可以在控制器中使用以下代码

在您的控制器中

$this->loadModel('YourModel');
if($this->YourModel->validates())
{
    $this->YourModel->save($this->data); 
}

在模型类中添加以下代码

var $validate = array(
        'first_name' => array(
            'notEmpty'  => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.'
            )
        ),
        'last_name' => array(
            'notEmpty'  => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.'
            )
        ),
        'phone' =>  array(
            'notEmpty'  => array(
                'rule' => 'notEmpty',
                'message' => 'Phone number should be valid.'
            ),
            'phone' => array(
                'rule' => array('phone', null, 'us'),
                'message' => 'Phone number should be valid e.g. 555-555-5555'
            )
        ),
        'email' => array(
            'email' => array(
                'rule' => 'email',
                'message' => 'Please enter a valid email address'
            ),
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank'
            ),
            'validEmail' => array(
                'rule' => array('validEmail'),
                'message' => 'Email address does not exist.'
            )
        ),
        'captcha_code' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank'
            )
        ),
        'address' => array(
            'notEmpty'  => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.'
            )
        ),
        'city' => array(
            'notEmpty'  => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.'
            )
        ),
        'street' => array(
            'notEmpty'  => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.'
            )
        )

    );