开发者

cakephp validation losing parameters

开发者 https://www.devze.com 2023-01-05 12:06 出处:网络
CONTROLLER class ImagesController extends AppController { var $name = \'Images\'; var $uses = array(\'Image\',\'Person\');

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

0

精彩评论

暂无评论...
验证码 换一张
取 消