Tutorial :cakephp validation losing parameters



Question:

CONTROLLER

class ImagesController extends AppController {      var $name = 'Images';      var $uses = array('Image','Person');      var $helpers = array('Html', 'Form');              function add($idp = 0, $mac = 0)           {                  if (!empty($this->data) &&                   is_uploaded_file($this->data['Image']['File']['tmp_name'])) {                  $fileData = fread(fopen($this->data['Image']['File']['tmp_name'], "r"),                                           $this->data['Image']['File']['size']);                    $this->data['Image']['name'] = $this->data['Image']['File']['name'];                  $this->data['Image']['type'] = $this->data['Image']['File']['type'];                  $this->data['Image']['size'] = $this->data['Image']['File']['size'];                  $this->data['Image']['data'] = $fileData;                  $this->data['Image']['people_id'] = $idp;                    $this->Image->save($this->data);                    $this->redirect(array('controller' => 'People', 'action' => 'welcome',$mac));                   }            }  

When I validate the form, I lose both parameters $idp and $mac.. How can I make them persist?

VIEW

<?php      echo $form->create('Image', array('action' => 'add', 'type' => 'file'));      echo $form->file('File');      echo $form->submit('Upload');      echo $form->end();  ?>  


Solution:1

You should add the following in your form element:

'url'=>$this->Html->url()  

So the form will look like:

echo $form->create('Image', array('type' => 'file', 'url'=>$this->Html->url()));  

Basically the $this->Html->url() get the current url with parameters and you pass it into the action of the form and if you submit in the same action (as it usually does) this should keep your parameters in tact.

HTH


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »