So, I found the bit of code in the FormHelper.
On Wednesday, 14 May 2014 09:26:52 UTC+10, Reuben wrote:
-- if ($model !== false && $key) {
$recordExists = (
isset($this->request->data[$model]) &&
!empty($this->request->data[$model][$key]) &&
!is_array($this->request->data[$model][$key])
);
if ($recordExists) {
$created = true;
$id = $this->request->data[$model][$key];
}
}
$options = array_merge(array(
'type' => ($created && empty($options['action'])) ? 'put' : 'post',
'action' => null,
'url' => null,
'default' => true,
'encoding' => strtolower(Configure::read('App.encoding')),
'inputDefaults' => array()),
$options);
Basically, if there is data set on the model that specifies the key for the model, and if you do not specify a specific action on the form, then it will be a PUT, rather than a POST.
Armed with this, I could go several ways.
1. Always specify an action on my forms, so I always get a POST. But I'm lazy, and don't want to write code if I don't have to.
2. Always test for PUT or POST in my controller, to handle either. That's extra code, too. But, a PUT is assumed to be for updates of existing entities. Conversely, a POST is either a create, or some other RPC. To determine which is which, for cases where my action handles creates and edits, I would need to check if the id of the model has been set. I do that in the first case scenario as well.
So, looks like I'll be going with case 1, so I only need to deal with POST. PUT can still be used (and should be used) for REST APIs, but for the most part, I only want to have to deal with POST methods.
On Wednesday, 14 May 2014 09:26:52 UTC+10, Reuben wrote:
Apparently, when it's a put!I've been struggling for a couple of hours to work out why my unit test for an edit action is working wonderfully, but it's not working via the browser.The URL is /users/edit/75, where 75 is the id of the user I'm editing.So, my unit test has $this->testAction('/users/edit/75', array('data' => $data, 'method' => 'post')) In my edit action, I do a if ($this->request->is('post')) to test if the form has been submitted, and process accordingly.In my view, I have echo $this->Form->create(null, array('id' => 'edituser')); to generate the form element. The generated element is <form action="/synapse/users/edit/78?pnumis=" id="edituser" method="post" accept-charset="utf-8"> After sticking a bunch of debugging through the code, I finally figure out that the condition is failing, because from the browser, $this->request->method() == 'PUT'!, even though the submit is actually a POST (from Chrome Network).It occurs to me that this is happening because I'm targeting a specific user to edit, and the typical REST action is a PUT, rather than POST (which would be used for a create).I'm going to do a little tracking down in the code to see if I can work out where/why this is happening, but anyone happens to have had experience with this, and what I should be doing instead, please let me know.RegardsReuben Helms
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment