php - Issue with CakePHP model validationErrors property -


i using cakephp 2.1.3 1 of project, want model validation errors returned in json format via controller, when try access validation errors via cakephp's model property validationerrors returns valid errors array when primary , other associated models has validation errors. when primary model valid , associated models has validation errors results invalid validation errors array. below example:

if ($this->request->is('post') || $this->request->is('put')) {     if ($this->primarymodel->saveall($this->request->data, array('validate' => 'first'))) {         echo json_encode(array('success' => true));     } else {         $errors=array();         $tablestomodels=$this->primarymodel->tabletomodel; //list of table names included in model description. used associations.         foreach($tablestomodels $tabel => $model) {             $errors[$model]=$this->{$model}->validationerrors;         }         pr($errors);         //echo json_encode(array('errors' => $errors));     }     exit; } 

the above code results following output if primary , associated models has validation errors:

array (     [primarymodel] => array         (             [field1] => array                 (                     [0] => notempty                 )          )      [associatedmodel] => array         (             [field1] => array                 (                     [0] => notempty                 )              [field2] => array                 (                     [0] => notempty                 )          ) ) 

and when fields filled in primary model, or primary model has no validation errors results following errors array:

array (     [primarymodel] => array         (             [associatedmodel] => array                 (                     [field1] => array                         (                             [0] => notempty                         )                      [field2] => array                         (                             [0] => notempty                         )                  )          )      [associatedmodel] => array         (             [field1] => array                 (                     [0] => notempty                 )              [field2] => array                 (                     [0] => notempty                 )          )  ) 

i don't know why happening , doing wrong?

here edit form code incase guys need sort out:

<php     echo $this->form->input('primarymodel.field1');     echo $this->form->input('primarymodel.field2');     echo $this->form->input('primarymodel.field3');     echo $this->form->input('primarymodel.field4');     echo $this->form->input('associatedmodel.field1');     echo $this->form->input('associatedmodel.field2');     echo $this->form->input('associatedmodel.field3');     echo $this->form->input('primarymodel.field5'); ?> 

thank in advance :)

the controller constructs $validationerrors property when renders action. instead of echoing json directly controller, should use json view. let view handle display logic, in case, displaying results json instead of regular html.

add extension routes file:

router::parseextensions('json'); 

then create view in /view/controller/json/action.ctp replacing 'controller' , 'action' controller , action name. view this:

echo json_encode($this->validationerrors); 

you need create json layout in /view/layouts/json/default.ctp.

echo $content_for_layout; 

and access normal action, using .json extension, http://example.com/controller/action.json.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -