CakePHP问题:我如何检查提交表单后是否有一个空字段


CakePHP question: How can i check whether there is an empty field or not after submitting form?

假设email字段为空。

  Array
 (
       [Comment] => Array
       (
          [post_id] => 10
          [name] => name6
          [email] => 
          [body] => body6
        )
 )

这是添加操作。

function add($id) {
    $temp = $this->data;
    debug($temp);
    if (!empty($this->data)) {
        $this->Comment->create();
        if ($this->Comment->save($this->data)) {
            $this->Session->setFlash('Your comment has been saved.');
            $this->redirect(array('controller'=>'posts','action' => 'index'));
        }
    }
    }

现在我如何检查电子邮件字段是否为空。如果任何字段为空,则它将显示消息并重定向到另一个操作。

function add($id) {
   if(!isset($this->data['Comment'][email]))
   {
        $this->Session->setFlash('Email is empty. Please try again !!');
        $this->redirect(array('controller'=>'posts','action' => 'index'));
   }

你的add代码在这里…

但是我建议你把所有的验证放在各自的模型中