CakePHP加载模型验证规则不起作用

时间:2015-04-16 07:55:08

标签: validation cakephp model

我在不同的控制器中加载模型。 我正在加载不同模型的控制器名称是 - " MembersController"我正在加载" Usermgmt"模型" UsermgmtController"。 " Usermgmt"模型的验证如下 -

public $validate = array(
        'email' => array(
            'valid' => array(
                'rule' => 'notEmpty',
                'required' => true,
                'allowEmpty' => false,
                'message' => 'Please enter a value for email.'
            ),
            'duplicate' => array(
                'rule' => 'isUnique',
                'on' => 'create',
                'message' => 'This email is already exist.'
            ),
            'duplicate1' => array(
                'rule' => 'email',
                'message' => 'Please enter valid email.'
            )
        ),
        'firstname' => array(
            'valid' => array(
                'rule' => 'notEmpty',
                'required' => true,
                'allowEmpty' => false,
                'message' => 'Please enter a value for first name.'
            )
        ),
        'username' => array(
            'valid' => array(
                'rule' => 'notEmpty',
                'required' => true,
                'allowEmpty' => false,
                'message' => 'Please enter a value for user name.'
            ),
            'duplicate' => array(
                'rule' => 'isUnique',
                'on' => 'create',
                'message' => 'This user is already exist.'
            )
        ),
        'password' => array(
            'valid' => array(
                'rule' => 'notEmpty',
                'required' => true,
                'allowEmpty' => false,
                'message' => 'Please enter a value for password.'
            )
        ),
        'confirm_password' => array(
            'valid' => array(
                'rule' => 'notEmpty',
                'required' => true,
                'allowEmpty' => false,
                'message' => 'Please enter a value for confirm password.'
            ),
            'duplicate2' => array(
                'rule' => 'matchpassword',
                'on' => 'create',
                'message' => 'Password must be same.'
            )
        )
    );

现在我正在以下列方式申请和加载模型。

$this->loadModel('Usermgmt');
            $this->Usermgmt->set($this->data);

            if ($this->Usermgmt->validates()) {
  if ($this->Usermgmt->save($data, true)) {
                    $userid = $this->Usermgmt->id;

                    $this->Session->setFlash('User has been added', 'success');

                }
}

但验证不起作用,它正在插入空值。

3 个答案:

答案 0 :(得分:0)

尝试uses加载模型

示例:

public $uses = array('Usermgmt');

答案 1 :(得分:0)

您使用的是$data的两个不同实例。即$this->data$data

您应该验证要保存的同一组数据,例如以下两个示例中的任何一个,无论哪个保存您的数据。

$this->loadModel('Usermgmt');
        $this->Usermgmt->set($data);

        if ($this->Usermgmt->validates()) {
if ($this->Usermgmt->save($data, true)) {
                $userid = $this->Usermgmt->id;

                $this->Session->setFlash('User has been added', 'success');

            }

}

或者

$this->loadModel('Usermgmt');
        $this->Usermgmt->set($this->data);

        if ($this->Usermgmt->validates()) {
if ($this->Usermgmt->save($this->data, true)) {
                $userid = $this->Usermgmt->id;

                $this->Session->setFlash('User has been added', 'success');

            }

}

答案 2 :(得分:0)

除非您在验证之后要求执行其他逻辑,否则在保存之前您可以取消显式调用validates()并只保存数据save()将自动为您验证数据。另外,我注意到你正在使用$this->data,看起来你正在使用CakePHP 2,在这种情况下你需要使用$this->request->data代替: -

$this->loadModel('Usermgmt');

if ($this->Usermgmt->save($this->request->data) !== false) {
    $this->Session->setFlash('User has been added', 'success');
}

如果仍然失败,请确保您没有以导致验证中断的方式覆盖模型中的beforeValidate()