Reputation: 672
(CakePHP 2.1, CentOS) Hey guys- so I'm doing something simple in CakePHP. I start a form using the FormHelper class from inside my edit.ctp view, using a familiar call:
echo $this->Form->create();
and when I go to mycoolsite/posts/edit/17 the markup contains
<form action="/posts/edit/17"
This is expected. Now, when I specify the model and the url, like this,
echo $this->Form->create('Post', array('url' => '/posts/edit'));
and again go to mycoolsite/posts/edit/17, I get this form tag in the markup, which is missing the "/17" from the action:
<form action="/newsite/posts/edit"
Is this normal? I could have sworn that the record id gets appended to the action automatically by the create function even when you specify the url parameter explicitly.
Upvotes: 0
Views: 446
Reputation: 672
Answer: After digging through the source code of the FormHelper class I've determined that this is expected behavior. In cases where the url is not passed in through the options array in the FormHelper's create() function, the code determines the action for the form with this handy call:
$this->url( $this->request->here(false) )
And you can use this code in any extension you make of the FormHelper class, which is what I'm doing. At any rate, this is what solved my problem.
Upvotes: 0
Reputation: 21743
Don't forget to add the id in the form:
echo $this->Form->create('Post');
echo $this->Form->input('id');
...
Then everythings works as it should - and as it's documented.
And don't mangle with the url if you don't have to. It will post to itself on its own :) What do you are doing there results in expected behavior: You overwrite the url to post to with an invalid one.
Also note that you should always prefer the array syntax over strings for flexibility. And no, it will not magically add ids/strings to string URLs.
EDIT: In case you have multiple forms per action do don't have to use different urls to post to. Only different forms/ids to make them unique (maybe also containing a unique form field with the "action" it is supposed to do and to distinguish forms). But if you really want different target URLs, make sure you generate them in a valid way:
echo $this->Form->create('Post', array('url' => array('action' => 'edit', $id)));
$id being the value passed down from the controller where you read the record and use its id value here to populate the form with the full url including the missing id.
Upvotes: 1