2012-11-01 52 views
-2

我可以这样做来搜索表中的电子邮件。在查询数据库之前使用模型进行验证

// 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')); 
       } 
      } 
     } 
    } 
} 

已经有人做过类似的,以我想要什么解决办法?

+0

倘使你写了一个简短的解释(而不仅仅是代码)你做了什么/希望做什么 – Dave

+0

我想我已经解释了很多,我已经验证,并且由于验证规则'isUnique'而失败,我想验证模型不查询数据库 –

回答

1

你可以从你的控制器做到这一点。

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

+1感谢您对此信息的了解 –

+0

这个问题不会增加验证错误 –

0

我同意@Dave,你不是很清楚你想做什么。首先你访问请求数据应该是$ this-> request-> data ['User'] ['email'];如果您以标准格式助手格式发布数据。

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

如果不是空的,邮件规则通过,最终你将不得不查询数据库发现,如果该地址已经存在。

+0

我不能同意你答案中较为清楚的部分,但我同意你对它的陈述,而不是查询。没有睡觉:p –

+0

好的。我可以绕过'isUnique'上的验证吗?因为我在'isUnique'上得到错误。 –

+0

是的,有一个部分用于动态更改蛋糕文档中的规则。但我认为你的方法是这里的问题,而不是蛋糕处理验证的方式。 http://book.cakephp.org/2.0/en/models/data-validation.html –