在查询DB之前使用模型进行验证

时间:2012-11-01 00:19:42

标签: php validation cakephp

我可以这样在桌子上搜索电子邮件。

// controller method
public function forgot_password() {
    if ($this->Session->read('Auth.User')) {
        $this->redirect(array('action' => 'add'));
    } else {
        if ($this->request->is('post') || $this->request->is('put')) {
            $user = $this->User->findByEmail($this->request->data('User.email'));
            if ($user) {
                    $this->request->data['User']['id'] = $user['User']['id'];
                    $this->request->data['User']['random_string'] = $this->String->random();
                    unset($this->request->data['User']['email']);
                    $this->User->save($this->request->data);
                    // $this->_sendEmail($user);
                    $this->Session->setFlash(__('Instructions has been sent to your email'), 'flash');
                    $this->redirect(array('action' => 'forgot_password'));

            } else {
                // passed! do stuff
            }
        }
    }
}


// validate in the model
public $validate = array(
    'email' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'An Email is required'
        ),
        'email' => array(
            'rule' => array('email'),
            'message' => 'Email is invalid'
        ),
        'isUnique' => array(
            'rule' => array('isUnique'),
            'message' => 'Email is already in use'
        )
    ),
    'password' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'A password is required'
        )
    ),
    'role' => array(
        'valid' => array(
            'rule' => array('inList', array('admin', 'author')),
            'message' => 'Please enter a valid role',
            'allowEmpty' => false
        )
    )
);

上面的代码工作正常。

我只是想在验证数据库之前验证电子邮件是有效的电子邮件还是空的。我想出了下面的那个。我遇到的问题是用$this->request->data设置用户。每当我验证时,它都会运行isUnique规则并失败。

public function forgot_password() {
    if ($this->Session->read('Auth.User')) {
        $this->redirect(array('action' => 'add'));
    } else {
        if ($this->request->is('post') || $this->request->is('put')) {
            $this->User->set($this->request->data);
            if ($this->User->validates()) {
                $user = $this->User->findByEmail($this->request->data('User.email'));
                if ($user) {
                    $this->request->data['User']['id'] = $user['User']['id'];
                    $this->request->data['User']['random_string'] = $this->String->random();
                    unset($this->request->data['User']['email']);
                    $this->User->save($this->request->data);
                    // $this->_sendEmail($user);
                    $this->Session->setFlash(__('Instructions has been sent to your email'), 'flash');
                    $this->redirect(array('action' => 'forgot_password'));
                }
            }
        }
    }
}

有人做过类似我想要的解决方案吗?

2 个答案:

答案 0 :(得分:1)

您可以从控制器执行此操作。

App::uses('Validation', 'Utility');
if (Validation::email($this->request->data('User.email'))) {
  ...
}

答案 1 :(得分:0)

我同意@Dave你不太清楚你想要做什么。

首先,您对请求数据的访问应该是$ this-> request-> data ['User'] ['email'];如果您以标准表格帮助格式发布数据。

如果值为空或电子邮件规则失败,我很确定在保存调用中查询数据库之前模型验证会失败。由于notEmpty是第一个规则,因此在查询数据库之前将由验证实用程序进行检查。如果失败,那么将跳过该字段并且不进行数据库调用(只要'last'不等于false。同样适用于电子邮件规则,因为cake使用该规则的正则表达式模式。您可以跟踪事件链通过查看Model.php中的保存功能。

如果不为空且电子邮件规则通过,最终您必须查询数据库以查找该地址是否已存在。

相关问题