Domanda

I am using Cakephp 2.4.5.

I have a controller which takes in an id and checks whether this id exists inside the Model. If it does not exist, I would like the controller to return a validation error message "id not found" in json when it returns the HTTP response. This controller takes in a normal HTTP POST which is not in json.

How can this be done in Cakephp 2.4.5?

My controller code looks something like this;

public function controller_function($id=null)
{
    if ($this->request->is('post')) 
    {
       $field=$this->request->data['Model']['field'];
       $Model_id = $this->Model->findFieldID($field);
       if (empty($Model_id) ) //record not found. Return validation error
       {
           //Send validation error back in JSON. How??
       }
    }
}
È stato utile?

Soluzione

You can do it like this:

echo json_encode(array('error_message' => 'id not found'));

And for the other stuff you are having problem, since you said there is no view, add this to your method:

$this->autoRender = false;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top