【问题标题】:CakePHP question: How can i check whether there is an empty field or not after submitting form?CakePHP 问题:提交表单后如何检查是否有空字段?
【发布时间】:2011-09-13 12:10:09
【问题描述】:

假设电子邮件字段为空。

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

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

【问题讨论】:

标签: php mysql cakephp cakephp-1.3


【解决方案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'));

   }

您的添加代码在这里...

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

【讨论】:

  • 我不想检查单个字段。我在评论模型中添加了验证。如何在不单独检查的情况下检查是否有空字段?
猜你喜欢
  • 2016-12-08
  • 1970-01-01
  • 1970-01-01
  • 2012-06-21
  • 1970-01-01
  • 2013-09-18
  • 2013-10-28
  • 1970-01-01
  • 2012-07-02
相关资源
最近更新 更多