cola
cola

Reputation: 12466

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

Suppose the email field is empty.

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

 )

This is the add action.

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

Now how can i check whether the email field empty or not. If any field is empty then it will display message and redirect to another action.

Upvotes: 0

Views: 2172

Answers (1)

Rikesh
Rikesh

Reputation: 26421

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

   }

Your add code goes here...

But i suggest you to put all validation in respective model.

Upvotes: 1

Related Questions