I wanted to now if there is a way to pass a simple string inside url and cakephp some how put it inside one of the inputs of my form without any code writing on view side? I tried calling this->set("bla","bla"); the field name is bla but nothing changed in view
2 Answers
As I understood the question you want to have something like this:
in the url you have something like:
http://yourserver.com/controller/action?search=string
and you want to put this "string" in the search field, right?
Then let's imagine that your field's name is data[search_field]. (I am skipping the model, because for my example it's not needed, but it's possible the name to be data[Model][search_field]).
Then in your controller's action you have to do following:
$this->data['search_string'] = $this->params['url']['search'];
1 Comment
Chen Kinnrot
Only one thing is missing, if form encounters a validation error after submit, this will fire a warning I needed to add the flollowing: if (isset($this->params['url']['subject'])) { echo $form->hidden('subject',array('default'=>$this->params['url']['subject'])); } else { echo $form->hidden('subject'); }
You can pass values in the url by using html helper. Try:
echo $this->Html->link('View Some Page', array(
'controller' => 'yourController',
'action' => 'view',
1, // id
'?' => array('yourField' => 'valueToPass'))
);
3 Comments
bancer
I did not understand your question. If you build a link using html helper you can pass any value of any parameter to any page you want.
Chen Kinnrot
Yes yes this is obvious, will cake know to put the data in the view field?
bancer
Try to do that. If it does not work then you will need to catch the value from url in the view (
$this->params['yourField']) and input it to the field as default value, for example book.cakephp.org/view/1402/options-default.