CakePHP: GET Form Does Not Automatically Fill Form Fields After Submission

I am using Cake 2.3.0. If I submit my form using POST , the fields of the selected form are transferred, however, if I submit my form using GET , all form fields will return to their default values.

Is there a way to make the GET view the same as POST ?

Here is 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()); } } 

This is what my mind 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 fine. But I need to do this via GET .

thanks

+4
source share
6 answers

I agree that this is annoying (IMO CakePHP should be smart enough to automatically detect β€œwhere” in order to get its data based on β€œtype”).

You will need to copy the β€œrequest” of the request into the β€œdata” of the request;

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

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

+3
source

Try adding this to the controller:

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

My decision:

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

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

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

Use PRG . Checkout this plugin.

0
source

What I ended up with is a loop through the array $this->request->query and pass these values ​​into the correspondence $this->request->data .

So, I added this:

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

What 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 am not going to accept this as an answer, though, since I do not know if this is the optimal or suggested way to do this. But now it works for me.

0
source

This works for me:

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

Shape Definition:

$ this-> form-> create ('cluster', array ('type' => 'get'));

0
source

All Articles