From 1c47b2182e06b2455be7bc55969044e24d3f1eeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Fri, 30 Oct 2009 00:32:54 -0430 Subject: [PATCH] Adding form encoding default in form create options. Default is always taken from Configure::read('App.encoding'). closes #6042 --- cake/libs/view/helpers/form.php | 7 ++ .../cases/libs/view/helpers/form.test.php | 104 ++++++++++++++---- 2 files changed, 87 insertions(+), 24 deletions(-) diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index 73915d738..6c0065c0c 100755 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -195,6 +195,7 @@ class FormHelper extends AppHelper { 'action' => null, 'url' => null, 'default' => true, + 'encoding' => strtolower(Configure::read('App.encoding')), 'inputDefaults' => array()), $options); $this->_inputDefaults = $options['inputDefaults']; @@ -256,6 +257,12 @@ class FormHelper extends AppHelper { $htmlAttributes['onsubmit'] = 'event.returnValue = false; return false;'; } } + + if (!empty($options['encoding'])) { + $htmlAttributes['accept-charset'] = $options['encoding']; + unset($options['encoding']); + } + unset($options['default']); $htmlAttributes = array_merge($options, $htmlAttributes); diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index 9365d7082..4619084e0 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -723,10 +723,10 @@ class FormHelperTest extends CakeTestCase { */ function testFormCreateWithSecurity() { $this->Form->params['_Token'] = array('key' => 'testKey'); - + $encoding = strtolower(Configure::read('App.encoding')); $result = $this->Form->create('Contact', array('url' => '/contacts/add')); $expected = array( - 'form' => array('method' => 'post', 'action' => '/contacts/add'), + 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding), 'fieldset' => array('style' => 'display:none;'), array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')), array('input' => array( @@ -734,7 +734,7 @@ class FormHelperTest extends CakeTestCase { )), '/fieldset' ); - $this->assertTags($result, $expected); + $this->assertTags($result, $expected,true); $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm')); $expected['form']['id'] = 'MyForm'; @@ -1078,8 +1078,9 @@ class FormHelperTest extends CakeTestCase { $this->Form->params['_Token']['key'] = 'testKey'; $result = $this->Form->create('Contact', array('url' => '/contacts/add')); + $encoding = strtolower(Configure::read('App.encoding')); $expected = array( - 'form' => array('method' => 'post', 'action' => '/contacts/add'), + 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding), 'fieldset' => array('style' => 'display:none;'), array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')), array('input' => array( @@ -1275,9 +1276,11 @@ class FormHelperTest extends CakeTestCase { $this->assertFalse($this->UserForm->OpenidUrl->validates()); $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login')); + $encoding = strtolower(Configure::read('App.encoding')); $expected = array( 'form' => array( - 'method' => 'post', 'action' => '/user_forms/login/', 'id' => 'UserFormLoginForm' + 'method' => 'post', 'action' => '/user_forms/login/', 'id' => 'UserFormLoginForm', + 'accept-charset' => $encoding ), 'fieldset' => array('style' => 'display:none;'), 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), @@ -1318,8 +1321,9 @@ class FormHelperTest extends CakeTestCase { $this->assertFalse($this->ValidateUser->ValidateProfile->validates()); $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add')); + $encoding = strtolower(Configure::read('App.encoding')); $expected = array( - 'form' => array('method' => 'post', 'action' => '/validate_users/add/', 'id'), + 'form' => array('method' => 'post', 'action' => '/validate_users/add/', 'id','accept-charset' => $encoding), 'fieldset' => array('style' => 'display:none;'), 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), '/fieldset' @@ -1361,8 +1365,9 @@ class FormHelperTest extends CakeTestCase { $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates()); $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add')); + $encoding = strtolower(Configure::read('App.encoding')); $expected = array( - 'form' => array('method' => 'post', 'action' => '/validate_users/add/', 'id'), + 'form' => array('method' => 'post', 'action' => '/validate_users/add/', 'id','accept-charset' => $encoding), 'fieldset' => array('style' => 'display:none;'), 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), '/fieldset' @@ -4636,9 +4641,11 @@ class FormHelperTest extends CakeTestCase { */ function testFormCreate() { $result = $this->Form->create('Contact'); + $encoding = strtolower(Configure::read('App.encoding')); $expected = array( 'form' => array( - 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add/' + 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add/', + 'accept-charset' => $encoding ), 'fieldset' => array('style' => 'preg:/display\s*\:\s*none;\s*/'), 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), @@ -4648,20 +4655,23 @@ class FormHelperTest extends CakeTestCase { $result = $this->Form->create('Contact', array('type' => 'GET')); $expected = array('form' => array( - 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add/' + 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add/', + 'accept-charset' => $encoding )); $this->assertTags($result, $expected); $result = $this->Form->create('Contact', array('type' => 'get')); $expected = array('form' => array( - 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add/' + 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add/', + 'accept-charset' => $encoding )); $this->assertTags($result, $expected); $result = $this->Form->create('Contact', array('type' => 'put')); $expected = array( 'form' => array( - 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add/' + 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add/', + 'accept-charset' => $encoding ), 'fieldset' => array('style' => 'preg:/display\s*\:\s*none;\s*/'), 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'), @@ -4674,7 +4684,8 @@ class FormHelperTest extends CakeTestCase { $result = $this->Form->create('Contact'); $expected = array( 'form' => array( - 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1' + 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1', + 'accept-charset' => $encoding ), 'fieldset' => array('style' => 'preg:/display\s*\:\s*none;\s*/'), 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'), @@ -4687,7 +4698,7 @@ class FormHelperTest extends CakeTestCase { $expected = array( 'form' => array( 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post', - 'action' => '/contact_non_standard_pks/edit/1' + 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding ), 'fieldset' => array('style' => 'preg:/display\s*\:\s*none;\s*/'), 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'), @@ -4697,7 +4708,10 @@ class FormHelperTest extends CakeTestCase { $result = $this->Form->create('Contact', array('id' => 'TestId')); $expected = array( - 'form' => array('id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1'), + 'form' => array( + 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1', + 'accept-charset' => $encoding + ), 'fieldset' => array('style' => 'preg:/display\s*\:\s*none;\s*/'), 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'), '/fieldset' @@ -4707,7 +4721,10 @@ class FormHelperTest extends CakeTestCase { $this->Form->params['action'] = 'add'; $result = $this->Form->create('User', array('url' => array('action' => 'login'))); $expected = array( - 'form' => array('id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login/'), + 'form' => array( + 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login/', + 'accept-charset' => $encoding + ), 'fieldset' => array('style' => 'preg:/display\s*\:\s*none;\s*/'), 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), '/fieldset' @@ -4717,7 +4734,8 @@ class FormHelperTest extends CakeTestCase { $result = $this->Form->create('User', array('action' => 'login')); $expected = array( 'form' => array( - 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login/' + 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login/', + 'accept-charset' => $encoding ), 'fieldset' => array('style' => 'preg:/display\s*\:\s*none;\s*/'), 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), @@ -4727,7 +4745,7 @@ class FormHelperTest extends CakeTestCase { $result = $this->Form->create('User', array('url' => '/users/login')); $expected = array( - 'form' => array('method' => 'post', 'action' => '/users/login'), + 'form' => array('method' => 'post', 'action' => '/users/login','accept-charset' => $encoding), 'fieldset' => array('style' => 'preg:/display\s*\:\s*none;\s*/'), 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), '/fieldset' @@ -4739,7 +4757,8 @@ class FormHelperTest extends CakeTestCase { $result = $this->Form->create('User', array('action' => 'signup')); $expected = array( 'form' => array( - 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup/' + 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup/', + 'accept-charset' => $encoding ), 'fieldset' => array('style' => 'preg:/display\s*\:\s*none;\s*/'), 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), @@ -4778,6 +4797,7 @@ class FormHelperTest extends CakeTestCase { * */ function testFormCreateQuerystringParams() { + $encoding = strtolower(Configure::read('App.encoding')); $result = $this->Form->create('Contact', array( 'type' => 'post', 'escape' => false, @@ -4791,7 +4811,8 @@ class FormHelperTest extends CakeTestCase { 'form' => array( 'id' => 'ContactAddForm', 'method' => 'post', - 'action' => '/controller/action/?param1=value1&param2=value2' + 'action' => '/controller/action/?param1=value1&param2=value2', + 'accept-charset' => $encoding ), 'fieldset' => array('style' => 'preg:/display\s*\:\s*none;\s*/'), 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), @@ -4807,14 +4828,16 @@ class FormHelperTest extends CakeTestCase { * @return void */ function testGetFormCreate() { + $encoding = strtolower(Configure::read('App.encoding')); $result = $this->Form->create('Contact', array('type' => 'get')); $this->assertTags($result, array('form' => array( - 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add/' + 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add/', + 'accept-charset' => $encoding ))); $result = $this->Form->text('Contact.name'); $this->assertTags($result, array('input' => array( - 'name' => 'name', 'type' => 'text', 'value' => '', 'id' => 'ContactName' + 'name' => 'name', 'type' => 'text', 'value' => '', 'id' => 'ContactName', ))); $result = $this->Form->password('password'); @@ -4875,10 +4898,12 @@ class FormHelperTest extends CakeTestCase { * @return void */ function testFormMagicInput() { + $encoding = strtolower(Configure::read('App.encoding')); $result = $this->Form->create('Contact'); $expected = array( 'form' => array( - 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add/' + 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add/', + 'accept-charset' => $encoding ), 'fieldset' => array('style' => 'preg:/display\s*\:\s*none;\s*/'), 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), @@ -5101,10 +5126,12 @@ class FormHelperTest extends CakeTestCase { * @return void */ function testForMagicInputNonExistingNorValidated() { + $encoding = strtolower(Configure::read('App.encoding')); $result = $this->Form->create('Contact'); $expected = array( 'form' => array( - 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add/' + 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add/', + 'accept-charset' => $encoding ), 'fieldset' => array('style' => 'preg:/display\s*\:\s*none;\s*/'), 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), @@ -5161,10 +5188,12 @@ class FormHelperTest extends CakeTestCase { * @return void */ function testFormMagicInputLabel() { + $encoding = strtolower(Configure::read('App.encoding')); $result = $this->Form->create('Contact'); $expected = array( 'form' => array( - 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add/' + 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add/', + 'accept-charset' => $encoding ), 'fieldset' => array('style' => 'preg:/display\s*\:\s*none;\s*/'), 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), @@ -5597,5 +5626,32 @@ class FormHelperTest extends CakeTestCase { $result = $this->Form->error('2.city'); $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div')); } + + function testInputErrorEscape() { + $this->Form->create('ValidateProfile'); + $this->Form->validationErrors['ValidateProfile']['city'] = 'required
'; + $result = $this->Form->input('city',array('error' => array('escape' => true))); + $this->assertPattern('/required<br>/', $result); + + $result = $this->Form->input('city',array('error' => array('escape' => false))); + $this->assertPattern('/required
/', $result); + } + + function testFormEncoding() { + $result = $this->Form->create('UserForm', array( + 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1' + ) + ); + $expected = array( + 'form' => array( + 'method' => 'post', 'action' => '/user_forms/login/', 'id' => 'UserFormLoginForm', + 'accept-charset' => 'iso-8859-1' + ), + 'fieldset' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), + '/fieldset' + ); + $this->assertTags($result, $expected); + } } ?> \ No newline at end of file