CakePHP问题:如何在提交表单后检查是否有空字段?

时间:2011-09-13 12:10:09

标签: php mysql cakephp cakephp-1.3

假设电子邮件字段为空。

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

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

1 个答案:

答案 0 :(得分:1)

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

   }

你的添加代码就在这里......

但我建议您将所有验证放在相应的模型中。

相关问题