Skip to content

Commit

Permalink
Merge pull request #7961 from cakephp/2.8-deprecate-action
Browse files Browse the repository at this point in the history
Deprecate action in Form::create()
  • Loading branch information
markstory committed Jan 7, 2016
2 parents 74b5fca + daca52e commit 6fbc029
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
41 changes: 25 additions & 16 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -1780,11 +1780,13 @@ public function testFormValidationAssociated() {
$this->assertFalse(empty($result));
$this->assertFalse($this->UserForm->OpenidUrl->validates());

$result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
$result = $this->Form->create('UserForm', array('type' => 'post', 'url' => array('action' => 'login')));
$encoding = strtolower(Configure::read('App.encoding'));
$expected = array(
'form' => array(
'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
'action' => '/user_forms/login',
'id' => 'UserFormLoginForm',
'method' => 'post',
'accept-charset' => $encoding
),
'div' => array('style' => 'display:none;'),
Expand Down Expand Up @@ -1822,7 +1824,7 @@ public function testFormValidationAssociatedFirstLevel() {
$this->assertFalse($this->ValidateUser->validates());
$this->assertFalse($this->ValidateUser->ValidateProfile->validates());

$result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
$result = $this->Form->create('ValidateUser', array('type' => 'post', 'url' => array('action' => 'add')));
$encoding = strtolower(Configure::read('App.encoding'));
$expected = array(
'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
Expand Down Expand Up @@ -1875,7 +1877,7 @@ public function testFormValidationAssociatedSecondLevel() {
$this->assertFalse($this->ValidateUser->ValidateProfile->validates());
$this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());

$result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
$result = $this->Form->create('ValidateUser', array('type' => 'post', 'url' => array('action' => 'add')));
$encoding = strtolower(Configure::read('App.encoding'));
$expected = array(
'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
Expand Down Expand Up @@ -8539,7 +8541,9 @@ public function testCreate() {
$result = $this->Form->create('User', array('url' => array('action' => 'login')));
$expected = array(
'form' => array(
'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
'action' => '/users/login',
'id' => 'UserLoginForm',
'method' => 'post',
'accept-charset' => $encoding
),
'div' => array('style' => 'display:none;'),
Expand All @@ -8548,7 +8552,7 @@ public function testCreate() {
);
$this->assertTags($result, $expected);

$result = $this->Form->create('User', array('action' => 'login'));
$result = $this->Form->create('User', array('url' => array('action' => 'login')));
$expected = array(
'form' => array(
'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
Expand All @@ -8562,15 +8566,20 @@ public function testCreate() {

$result = $this->Form->create('User', array('url' => '/users/login'));
$expected = array(
'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
'form' => array(
'action' => '/users/login',
'id' => 'UserAddForm',
'method' => 'post',
'accept-charset' => $encoding
),
'div' => array('style' => 'display:none;'),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
'/div'
);
$this->assertTags($result, $expected);

$this->Form->request['controller'] = 'pages';
$result = $this->Form->create('User', array('action' => 'signup'));
$result = $this->Form->create('User', array('url' => array('action' => 'signup')));
$expected = array(
'form' => array(
'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
Expand All @@ -8588,7 +8597,7 @@ public function testCreate() {
$result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
$expected = array(
'form' => array(
'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
'id' => 'ContactIndexForm', 'method' => 'post', 'action' => '/contacts/index/param',
'accept-charset' => 'utf-8'
),
'div' => array('style' => 'display:none;'),
Expand All @@ -8610,7 +8619,7 @@ public function testCreateOnSubmit() {
$result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
$expected = array(
'form' => array(
'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
'id' => 'ContactIndexForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
'accept-charset' => 'utf-8'
),
'div' => array('style' => 'display:none;'),
Expand All @@ -8630,7 +8639,7 @@ public function testCreateOnSubmit() {

$expected = array(
'form' => array(
'id' => 'ContactAddForm', 'method' => 'post',
'id' => 'ContactIndexForm', 'method' => 'post',
'onsubmit' => 'someFunction();event.returnValue = false; return false;',
'action' => '/contacts/index/param',
'accept-charset' => 'utf-8'
Expand Down Expand Up @@ -8688,7 +8697,7 @@ public function testCreateCustomRoute() {
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
$encoding = strtolower(Configure::read('App.encoding'));

$result = $this->Form->create('User', array('action' => 'login'));
$result = $this->Form->create('User', array('url' => array('action' => 'login')));
$expected = array(
'form' => array(
'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
Expand Down Expand Up @@ -8760,7 +8769,7 @@ public function testCreateWithInputDefaults() {
*/
public function testCreateWithAcceptCharset() {
$result = $this->Form->create('UserForm', array(
'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
'type' => 'post', 'url' => array('action' => 'login'), 'encoding' => 'iso-8859-1'
)
);
$expected = array(
Expand Down Expand Up @@ -8793,7 +8802,7 @@ public function testCreateQuerystringrequest() {
));
$expected = array(
'form' => array(
'id' => 'ContactAddForm',
'id' => 'ContactActionForm',
'method' => 'post',
'action' => '/controller/action?param1=value1&param2=value2',
'accept-charset' => $encoding
Expand All @@ -8814,7 +8823,7 @@ public function testCreateQuerystringrequest() {
));
$expected = array(
'form' => array(
'id' => 'ContactAddForm',
'id' => 'ContactActionForm',
'method' => 'post',
'action' => '/controller/action?param1=value1&param2=value2',
'accept-charset' => $encoding
Expand Down Expand Up @@ -8870,7 +8879,7 @@ public function testCreatePassedArgs() {
));
$expected = array(
'form' => array(
'id' => 'ContactAddForm',
'id' => 'ContactEditForm',
'method' => 'post',
'action' => '/contacts/edit/0/myparam',
'accept-charset' => $encoding
Expand Down
15 changes: 11 additions & 4 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -312,19 +312,19 @@ public function tagIsInvalid() {
* ### Options:
*
* - `type` Form method defaults to POST
* - `action` The controller action the form submits to, (optional).
* - `action` The controller action the form submits to, (optional). Deprecated since 2.8, use `url`.
* - `url` The URL the form submits to. Can be a string or a URL array. If you use 'url'
* you should leave 'action' undefined.
* - `default` Allows for the creation of Ajax forms. Set this to false to prevent the default event handler.
* - `default` Allows for the creation of AJAX forms. Set this to false to prevent the default event handler.
* Will create an onsubmit attribute if it doesn't not exist. If it does, default action suppression
* will be appended.
* - `onsubmit` Used in conjunction with 'default' to create ajax forms.
* - `onsubmit` Used in conjunction with 'default' to create AJAX forms.
* - `inputDefaults` set the default $options for FormHelper::input(). Any options that would
* be set when using FormHelper::input() can be set here. Options set with `inputDefaults`
* can be overridden when calling input()
* - `encoding` Set the accept-charset encoding for the form. Defaults to `Configure::read('App.encoding')`
*
* @param mixed $model The model name for which the form is being defined. Should
* @param mixed|null $model The model name for which the form is being defined. Should
* include the plugin name for plugin models. e.g. `ContactManager.Contact`.
* If an array is passed and $options argument is empty, the array will be used as options.
* If `false` no model is used.
Expand Down Expand Up @@ -379,6 +379,13 @@ public function create($model = null, $options = array()) {
$this->inputDefaults($options['inputDefaults']);
unset($options['inputDefaults']);

if (isset($options['action'])) {
trigger_error('Using key `action` is deprecated, use `url` directly instead.', E_USER_DEPRECATED);
}
if (is_array($options['url']) && isset($options['url']['action'])) {
$options['action'] = $options['url']['action'];
}

if (!isset($options['id'])) {
$domId = isset($options['action']) ? $options['action'] : $this->request['action'];
$options['id'] = $this->domId($domId . 'Form');
Expand Down

0 comments on commit 6fbc029

Please sign in to comment.