2011-09-13 24 views
0

假設電子郵件字段爲空。CakePHP問題:如何在提交表單後檢查是否有空字段?

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

查看http://book.cakephp.org/view/1173/notEmpty – JJJ

回答

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

    } 

你添加的代碼放在這裏......

但我建議你把所有的驗證在相應的模型。

+1

我不想檢查個別字段。我在評論模型中添加了驗證。我怎樣才能檢查是否有一個空的領域沒有單獨檢查他們? – shibly

相關問題