2011-09-13 7 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

私は個々のフィールドをチェックしたくありません。 Commentモデルで検証を追加しました。個別にチェックすることなく、空のフィールドがあるかどうかを確認する方法はありますか? – shibly

関連する問題