简体   繁体   中英

CakePHP: “GET” form does not auto-populate form fields after submission

I'm using Cake 2.3.0. If I submit my form using POST , the selected form fields carry over however if I submit my form using GET , all of the form fields return to their default values.

Is there a way to make the GET submission to work like that of the POST ?

Here's my contorller:

class ListingsController extends AppController {
    public function results() {
        $conditions = array(
            'Listing.Beds >=' => $this->request->query['beds'], 
            'Listing.ListingStatus >=' => $this->request->query['status'], 
        );

        $this->paginate = array(
            'conditions' => $conditions,
        );    

        $this->set('listings', $this->paginate());
    }
}

Here's what my view looks like.

echo $this->Form->create(null, array(
    'controller' => 'listings', 
    'action' => 'results',
    'type' => 'get'
));

echo $this->Form->input('name');

$beds = array('1' => '1+', '2' => '2+', '3' => '3+', '4' => '4+', '5' => '5+');
echo $this->Form->input('beds', array('options' => $beds));

$status = array('Active' => 'Active', 'Pending' => 'Pending', 'ActivePending' => 'Active and Pending');
echo $this->Form->input('status', array('options' => $status));

echo $this->Form->end('Update'); 

So basically if I change 'type' => 'get' to 'type' => 'post' it works just fine. But I need to be able to do this via GET .

Thanks

I agree that this is annoying (IMO CakePHP should be smart enough to automatically determin 'where' to get its data from, based on the 'type').

You'll have to copy the 'query' of the request to the 'data' of the request;

$this->request->data = $this->request->query;

Not behind my computer to test it (as usual, lol), but should probably work.

尝试将此添加到您的控制器:

$this->request->data = $this->params['url'];

My solution:

$this->params->data = array('Tablefilter' => $this->params->query);

where "Tablefilter" depends on form definition (usually the model name)

$this->Form->create('Tablefilter', array('type' => 'get'))

Use PRG . Checkout this plugin.

What I ended up doing was looping through the $this->request->query array and pass those values to the matching $this->request->data .

So I added this:

foreach($this->request->query as $k => $v){
    $this->request->data['Listing'][$k] = $this->request->query[$k];
}

Which ultimately gave me this:

class ListingsController extends AppController {
    public function results() {

        foreach($this->request->query as $k => $v){
            $this->request->data['Listing'][$k] = $this->request->query[$k];
        }

        $conditions = array(
            'Listing.Beds >=' => $this->request->query['beds'], 
            'Listing.ListingStatus >=' => $this->request->query['status'], 
        );

        $this->paginate = array(
            'conditions' => $conditions,
        );    

        $this->set('listings', $this->paginate());
    }
}

I'm not going to accept this as the answer though as I don't know if this is the optimal or suggested way to do it. But it works for me for now.

This works for me :

$this->request->data['Cluster'] = $this->params->query ; //controller side

Form definition:

$this->Form->create('Cluster',array('type'=>'get'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM