diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index 0dbaec02ca0..ecf4ede207a 100644 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -173,8 +173,11 @@ function _isRequiredField($validateProperties) { * * - `type` Form method defaults to POST * - `action` The controller action the form submits to, (optional). - * - `url` The url the form submits to. Can be a string or a url array, - * - `default` Allows for the creation of Ajax forms. + * - `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. + * 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. * - `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` @@ -294,19 +297,18 @@ function create($model = null, $options = array()) { unset($options['type'], $options['action']); if ($options['default'] == false) { - if (isset($htmlAttributes['onSubmit']) || isset($htmlAttributes['onsubmit'])) { - $htmlAttributes['onsubmit'] .= ' event.returnValue = false; return false;'; - } else { - $htmlAttributes['onsubmit'] = 'event.returnValue = false; return false;'; + if (!isset($options['onsubmit'])) { + $options['onsubmit'] = ''; } + $htmlAttributes['onsubmit'] = $options['onsubmit'] . 'event.returnValue = false; return false;'; } + unset($options['default']); if (!empty($options['encoding'])) { $htmlAttributes['accept-charset'] = $options['encoding']; unset($options['encoding']); } - unset($options['default']); $htmlAttributes = array_merge($options, $htmlAttributes); $this->fields = array(); diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index a2e3684bdaf..c1049ec67b5 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -5756,9 +5756,17 @@ function testCreate() { ); $this->assertTags($result, $expected); - $this->Form->request->data = array(); - $this->Form->request['controller'] = 'contacts'; - $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact')); + } + +/** + * Test the onsubmit option for create() + * + * @return void + */ + public function testCreateOnSubmit() { + $this->Form->data = array(); + $this->Form->params['controller'] = 'contacts'; + $this->Form->params['models'] = array('Contact'); $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false)); $expected = array( 'form' => array( @@ -5771,14 +5779,20 @@ function testCreate() { ); $this->assertTags($result, $expected); - $this->Form->request->data = array(); - $this->Form->request['controller'] = 'contacts'; - $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();')); + $this->Form->data = array(); + $this->Form->params['controller'] = 'contacts'; + $this->Form->params['models'] = array('Contact'); + $result = $this->Form->create(array( + 'url' => array('action' => 'index', 'param'), + 'default' => false, + 'onsubmit' => 'someFunction();' + )); $expected = 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' ), 'div' => array('style' => 'display:none;'),