Fix create() and onsubmit option.

Removing support for onSubmit variant.  Additional
choices are not necessary. Fixing onsubmit option
not correctly working.
Fixes #1977
This commit is contained in:
mark_story 2011-09-07 20:57:28 -04:00
parent 3c8c2936b3
commit 6ab59a447c
2 changed files with 23 additions and 8 deletions

View file

@ -6074,6 +6074,14 @@ class FormHelperTest extends CakeTestCase {
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
}
/**
* Test the onsubmit option for create()
*
* @return void
*/
public function testCreateOnSubmit() {
$this->Form->request->data = array(); $this->Form->request->data = array();
$this->Form->request['controller'] = 'contacts'; $this->Form->request['controller'] = 'contacts';
$this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact')); $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
@ -6092,11 +6100,17 @@ class FormHelperTest extends CakeTestCase {
$this->Form->request->data = array(); $this->Form->request->data = array();
$this->Form->request['controller'] = 'contacts'; $this->Form->request['controller'] = 'contacts';
$this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact')); $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
$result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false, 'onsubmit' => 'someFunction();')); $result = $this->Form->create(array(
'url' => array('action' => 'index', 'param'),
'default' => false,
'onsubmit' => 'someFunction();'
));
$expected = array( $expected = array(
'form' => array( 'form' => array(
'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'someFunction(); event.returnValue = false; return false;', 'action' => '/contacts/index/param', 'id' => 'ContactAddForm', 'method' => 'post',
'onsubmit' => 'someFunction();event.returnValue = false; return false;',
'action' => '/contacts/index/param',
'accept-charset' => 'utf-8' 'accept-charset' => 'utf-8'
), ),
'div' => array('style' => 'display:none;'), 'div' => array('style' => 'display:none;'),

View file

@ -302,7 +302,9 @@ class FormHelper extends AppHelper {
* - `action` The controller action the form submits to, (optional). * - `action` The controller action the form submits to, (optional).
* - `url` The url the form submits to. Can be a string or a url array. If you 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. * you should leave 'action' undefined.
* - `default` Allows for the creation of Ajax forms. * - `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 * - `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` * be set when using FormHelper::input() can be set here. Options set with `inputDefaults`
@ -421,19 +423,18 @@ class FormHelper extends AppHelper {
unset($options['type'], $options['action']); unset($options['type'], $options['action']);
if ($options['default'] == false) { if ($options['default'] == false) {
if (isset($htmlAttributes['onSubmit']) || isset($htmlAttributes['onsubmit'])) { if (!isset($options['onsubmit'])) {
$htmlAttributes['onsubmit'] .= ' event.returnValue = false; return false;'; $options['onsubmit'] = '';
} else {
$htmlAttributes['onsubmit'] = 'event.returnValue = false; return false;';
} }
$htmlAttributes['onsubmit'] = $options['onsubmit'] . 'event.returnValue = false; return false;';
} }
unset($options['default']);
if (!empty($options['encoding'])) { if (!empty($options['encoding'])) {
$htmlAttributes['accept-charset'] = $options['encoding']; $htmlAttributes['accept-charset'] = $options['encoding'];
unset($options['encoding']); unset($options['encoding']);
} }
unset($options['default']);
$htmlAttributes = array_merge($options, $htmlAttributes); $htmlAttributes = array_merge($options, $htmlAttributes);
$this->fields = array(); $this->fields = array();