php - CakePHP HABTM data not saving to database -


i have model genform has habtm relationship model pdffile. use generate list of checkboxes in genform index view. in genform model, added:

public $hasandbelongstomany = array(     'pdffile' => array(         'classname' => 'pdffile',         'jointable' => 'gen_forms_x_pdf_files'     ) 

here's fragment genform index.ctp view:

<?php  echo $this->form->input( 'pdffile', array('label' => 'select pdf files', 'multiple' => 'checkbox') ); echo $this->form->input( 'first_name' ); echo $this->form->input( 'last_name' ); ?> 

in controller, have basic save:

    if ($this->request->is('post')) { // form submitted         $this->genform->create();         if ($this->genform->save($this->request->data)) {             return $this->redirect(array('action' => 'generate', $this->genform->id)); // assemble pdf record         } else {             $this->session->setflash(__('log entry not saved.'));         }     } 

now $this->data looks when debug() it:

array(     'pdffile' => array(         'pdffile' => array(             (int) 0 => '1',             (int) 1 => '5'         )     ),     'genform' => array(         'first_name' => 'xxx',         'last_name' => 'xxx',         'association_id' => '1',         'email' => ''     ) ) 

everything works perfectly, couldn't validate checkboxes (at least 1 must checked). so, per this answer, made changes.

the index.ctp view became:

<?php  echo $this->form->input( 'genform.pdffile', array('label' => 'select pdf files', 'multiple' => 'checkbox') ); echo $this->form->input( 'first_name' ); echo $this->form->input( 'last_name' ); ?> 

here's validation rule:

public $validate = array(     'pdffile' => array(         'rule' => array(             'multiple', array('min' => 1)         ),          'message' => 'please select 1 or more pdfs'     ) ) 

this $this->data looks now:

array(     'genform' => array(         'pdffile' => array(             (int) 0 => '1',             (int) 1 => '5'         ),         'first_name' => 'xxx',         'last_name' => 'xxx',         'association_id' => '1',         'email' => ''     ) ) 

now checkboxes pdffile validate, pdffile data isn't saved -- although other fields genform saved correctly own table.

can tell me i'm missing pdffile saves automatically and gets validated?

the first form right

stating obvious, form worked form use i.e.:

echo $this->form->input('pdffile', array(     'label' => 'select pdf files',      'multiple' => 'checkbox' )); 

changing form have "field" named 'pdffile' not work - model layer remove data fields don't exist - , in form check gen_forms.pdffile, find there no field , ignore data.

validation

to take care of validation errors - use validation rule running on model checks number of habtm records saved. doesn't matter name of field used validation e.g.:

<?php class genform extends appmodel {      public $validate = array(         'dummy' => array( // <- name whatever             'atleastone' => array(                 'required' => true, // run                 'rule' => array('validateatleastone')             )         )     );      function validateatleastone() {         if (!isset($this->data['pdffile'])) {             // there no pdf data @ all, ignore rule             // allow other save operations work             return true;         }          $return = count(array_filter($this->data['pdffile']['pdffile']));         if (!$return) {             $this->pdffile->invalidate('pdffile', 'please upload file');         }         return $return;     }  } 

because validation rule returns false if there no records halt save. calling invalidate on habtm association same "field" name form helper - error message displayed.

alternatively

you can use second approach in question:

echo $this->form->input('genform.pdffile', array(     'label' => 'select pdf files',      'multiple' => 'checkbox' )); 

in full knowledge not how cake expects receive data , manipulate right format in beforevalidate:

<?php class genform extends appmodel {      public $validate = array(         'pdffile' => array( // existing rule             ...         )     );      function beforevalidate() {         if (isset($this->data[$this->alias]['pdffile'])) {             // keep existing data that's validation check against             // copy right location cake process             $this->data['pdffile']['pdffile'] = $this->data[$this->alias]['pdffile'];         }         return true;     }      ... } 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -