cakephp validation losing parameters
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_na开发者_C百科me'])) {
$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();
?>
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
精彩评论