Permalink
Browse files

Update checkbox() and tests to reflect new widget class.

* id attributes are not generated for all inputs now.
* Remove duplicated tests.
  • Loading branch information...
1 parent 3c1adc2 commit 4ef71460bd007a84bb4bd75e285d8055bfd7b7e3 @markstory markstory committed Feb 16, 2014
Showing with 63 additions and 178 deletions.
  1. +23 −30 src/View/Helper/FormHelper.php
  2. +40 −148 tests/TestCase/View/Helper/FormHelperTest.php
View
53 src/View/Helper/FormHelper.php
@@ -169,6 +169,7 @@ class FormHelper extends Helper {
* @var array
*/
protected $_defaultTemplates = [
+ 'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
'formstart' => '<form{{attrs}}>',
'formend' => '</form>',
'hiddenblock' => '<div style="display:none;">{{content}}</div>',
@@ -566,10 +567,8 @@ public function unlockField($name = null) {
* @param mixed $value Field value, if value should not be tampered with.
* @return mixed|null Not used yet
*/
- protected function _secure($lock, $field = null, $value = null) {
- if (!$field) {
- $field = $this->entity();
- } elseif (is_string($field)) {
+ protected function _secure($lock, $field, $value = null) {
+ if (is_string($field)) {
$field = Hash::filter(explode('.', $field));
}
@@ -1356,27 +1355,18 @@ protected function _inputLabel($fieldName, $label, $options) {
* @return string An HTML text input element.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
*/
- public function checkbox($fieldName, $options = array()) {
- $valueOptions = array();
- if (isset($options['default'])) {
- $valueOptions['default'] = $options['default'];
- unset($options['default']);
- }
+ public function checkbox($fieldName, $options = []) {
+ $options += array('hiddenField' => true, 'value' => 1);
- $options += array('value' => 1, 'required' => false);
- $options = $this->_initInputField($fieldName, $options) + array('hiddenField' => true);
- $value = current($this->value($valueOptions));
- $output = '';
+ // Work around value=>val translations.
+ $value = $options['value'];
+ unset($options['value']);
+ $options = $this->_initInputField($fieldName, $options);
+ $options['value'] = $value;
- if (
- (!isset($options['checked']) && !empty($value) && $value == $options['value']) ||
- !empty($options['checked'])
- ) {
- $options['checked'] = 'checked';
- }
+ $output = '';
if ($options['hiddenField']) {
$hiddenOptions = array(
- 'id' => $options['id'] . '_',
'name' => $options['name'],
'value' => ($options['hiddenField'] !== true ? $options['hiddenField'] : '0'),
'secure' => false
@@ -1387,8 +1377,7 @@ public function checkbox($fieldName, $options = array()) {
$output = $this->hidden($fieldName, $hiddenOptions);
}
unset($options['hiddenField']);
-
- return $output . $this->Html->useTag('checkbox', $options['name'], array_diff_key($options, array('name' => null)));
+ return $output . $this->widget('checkbox', $options);
}
/**
@@ -1515,7 +1504,7 @@ public function hidden($fieldName, $options = array()) {
));
if ($secure === true) {
- $this->_secure(true, null, '' . $options['val']);
+ $this->_secure(true, $options['name'], '' . $options['val']);
}
$options['type'] = 'hidden';
@@ -2761,7 +2750,7 @@ protected function _generateOptions($name, $options = array()) {
* @return array Array of options for the input.
*/
protected function _initInputField($field, $options = []) {
- $secure = !empty($this->request['_Token']);
+ $secure = !empty($this->request->params['_Token']);
if (isset($options['secure'])) {
$secure = $options['secure'];
unset($options['secure']);
@@ -2821,11 +2810,15 @@ protected function _initInputField($field, $options = []) {
* @return string|null
*/
protected function _secureFieldName($options) {
- if (isset($options['name'])) {
- preg_match_all('/\[(.*?)\]/', $options['name'], $matches);
- if (isset($matches[1])) {
- return $matches[1];
- }
+ if (!isset($options['name'])) {
+ return null;
+ }
+ if (strpos($options['name'], '[') === false) {
+ return [$options['name']];
+ }
+ preg_match_all('/\[(.*?)\]/', $options['name'], $matches);
+ if (isset($matches[1])) {
+ return $matches[1];
}
return null;
}
View
188 tests/TestCase/View/Helper/FormHelperTest.php
@@ -1214,9 +1214,8 @@ public function testDuplicateFieldNameResolution() {
* @return void
*/
public function testNoCheckboxLocking() {
- $this->markTestIncomplete('Need to revisit once models work again.');
- $this->Form->request->params['_csrfToken'] = 'foo';
- $this->assertSame(array(), $this->Form->fields);
+ $this->Form->request->params['_Token'] = 'foo';
+ $this->assertSame([], $this->Form->fields);
$this->Form->checkbox('check', array('value' => '1'));
$this->assertSame($this->Form->fields, array('check'));
@@ -5617,161 +5616,66 @@ public function testSelectCheckboxMultipleId() {
* @return void
*/
public function testCheckbox() {
- $this->markTestIncomplete('Need to revisit once models work again.');
$result = $this->Form->checkbox('Model.field');
$expected = array(
- 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
- array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField'))
+ 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
+ array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'))
);
$this->assertTags($result, $expected);
$result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
$expected = array(
- 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'theID_'),
+ 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => 'myvalue', 'id' => 'theID'))
);
$this->assertTags($result, $expected);
-
- $Contact->validationErrors['field'] = 1;
- $this->Form->request->data['Contact']['field'] = 'myvalue';
- $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
- $expected = array(
- 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'theID_'),
- array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
- );
- $this->assertTags($result, $expected);
-
- $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
- $expected = array(
- 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'ContactField_'),
- array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
- );
- $this->assertTags($result, $expected);
-
- $this->Form->request->data['Contact']['field'] = '';
- $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
- $expected = array(
- 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'theID_'),
- array('input' => array('type' => 'checkbox', 'name' => 'Contact[field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
- );
- $this->assertTags($result, $expected);
-
- $Contact->validationErrors = array();
- $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
- $expected = array(
- 'input' => array('type' => 'hidden', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'ContactField_'),
- array('input' => array('type' => 'checkbox', 'name' => 'Contact[field]', 'value' => 'myvalue', 'id' => 'ContactField'))
- );
- $this->assertTags($result, $expected);
-
- $this->Form->request->data['Contact']['published'] = 1;
- $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
- $expected = array(
- 'input' => array('type' => 'hidden', 'name' => 'Contact[published]', 'value' => '0', 'id' => 'theID_'),
- array('input' => array('type' => 'checkbox', 'name' => 'Contact[published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
- );
- $this->assertTags($result, $expected);
-
- $this->Form->request->data['Contact']['published'] = 0;
- $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
- $expected = array(
- 'input' => array('type' => 'hidden', 'name' => 'Contact[published]', 'value' => '0', 'id' => 'theID_'),
- array('input' => array('type' => 'checkbox', 'name' => 'Contact[published]', 'value' => '1', 'id' => 'theID'))
- );
- $this->assertTags($result, $expected);
-
- $result = $this->Form->checkbox('Model.CustomField.1.value');
- $expected = array(
- 'input' => array('type' => 'hidden', 'name' => 'Model[CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
- array('input' => array('type' => 'checkbox', 'name' => 'Model[CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
- );
- $this->assertTags($result, $expected);
-
- $result = $this->Form->checkbox('CustomField.1.value');
- $expected = array(
- 'input' => array('type' => 'hidden', 'name' => 'CustomField[1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
- array('input' => array('type' => 'checkbox', 'name' => 'CustomField[1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
- );
- $this->assertTags($result, $expected);
- }
-
-/**
- * test checkbox() with a custom name attribute
- *
- * @return void
- */
- public function testCheckboxCustomNameAttribute() {
- $this->markTestIncomplete('Need to revisit once models work again.');
- $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
- $expected = array(
- 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
- array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
- );
- $this->assertTags($result, $expected);
}
/**
- * test the checked option for checkboxes.
+ * Test checkbox being checked or having errors.
*
* @return void
*/
- public function testCheckboxCheckedOption() {
- $this->markTestIncomplete('Need to revisit once models work again.');
- $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
- $expected = array(
- 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
- array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
- );
- $this->assertTags($result, $expected);
-
- $result = $this->Form->checkbox('Model.field', array('checked' => 1));
- $expected = array(
- 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
- array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
- );
- $this->assertTags($result, $expected);
-
- $result = $this->Form->checkbox('Model.field', array('checked' => true));
- $expected = array(
- 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
- array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
- );
- $this->assertTags($result, $expected);
+ public function testCheckboxCheckedAndError() {
+ $this->article['errors'] = [
+ 'published' => true
+ ];
+ $this->Form->request->data['published'] = 'myvalue';
+ $this->Form->create($this->article);
- $result = $this->Form->checkbox('Model.field', array('checked' => false));
+ $result = $this->Form->checkbox('published', array('id' => 'theID', 'value' => 'myvalue'));
$expected = array(
- 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
- array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField'))
+ 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
+ array('input' => array(
+ 'type' => 'checkbox',
+ 'name' => 'published',
+ 'value' => 'myvalue',
+ 'id' => 'theID',
+ 'checked' => 'checked',
+ 'class' => 'form-error'
+ ))
);
$this->assertTags($result, $expected);
- $this->Form->request->data['Model']['field'] = 1;
- $result = $this->Form->checkbox('Model.field', array('checked' => false));
+ $this->Form->request->data['published'] = '';
+ $result = $this->Form->checkbox('published');
$expected = array(
- 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
- array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField'))
+ 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
+ array('input' => array('type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error'))
);
$this->assertTags($result, $expected);
}
/**
- * Test that disabled attribute works on both the checkbox and hidden input.
+ * test checkbox() with a custom name attribute
*
* @return void
*/
- public function testCheckboxDisabling() {
- $this->markTestIncomplete('Need to revisit once models work again.');
- $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
- $expected = array(
- array('input' => array('type' => 'hidden', 'name' => 'Account[show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
- array('input' => array('type' => 'checkbox', 'name' => 'Account[show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
- );
- $this->assertTags($result, $expected);
-
- $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
+ public function testCheckboxCustomNameAttribute() {
+ $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
$expected = array(
- array('input' => array('type' => 'hidden', 'name' => 'Account[show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
- array('input' => array('type' => 'checkbox', 'name' => 'Account[show_name]', 'value' => '1', 'id' => 'AccountShowName'))
+ 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0'),
+ array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1'))
);
$this->assertTags($result, $expected);
}
@@ -5783,43 +5687,31 @@ public function testCheckboxDisabling() {
* @return void
*/
public function testCheckboxHiddenField() {
- $this->markTestIncomplete('Need to revisit once models work again.');
- $result = $this->Form->input('UserForm.something', array(
- 'type' => 'checkbox',
+ $result = $this->Form->checkbox('UserForm.something', array(
'hiddenField' => false
));
$expected = array(
- 'div' => array('class' => 'input checkbox'),
- array('input' => array(
- 'type' => 'checkbox', 'name' => 'UserForm[something]',
- 'value' => '1', 'id' => 'UserFormSomething'
- )),
- 'label' => array('for' => 'UserFormSomething'),
- 'Something',
- '/label',
- '/div'
+ 'input' => array(
+ 'type' => 'checkbox',
+ 'name' => 'UserForm[something]',
+ 'value' => '1'
+ ),
);
$this->assertTags($result, $expected);
- $result = $this->Form->input('UserForm.something', array(
- 'type' => 'checkbox',
+ $result = $this->Form->checkbox('UserForm.something', array(
'value' => 'Y',
'hiddenField' => 'N',
));
$expected = array(
- 'div' => array('class' => 'input checkbox'),
array('input' => array(
'type' => 'hidden', 'name' => 'UserForm[something]',
- 'value' => 'N', 'id' => 'UserFormSomething_'
+ 'value' => 'N'
)),
array('input' => array(
'type' => 'checkbox', 'name' => 'UserForm[something]',
- 'value' => 'Y', 'id' => 'UserFormSomething'
+ 'value' => 'Y'
)),
- 'label' => array('for' => 'UserFormSomething'),
- 'Something',
- '/label',
- '/div'
);
$this->assertTags($result, $expected);
}

0 comments on commit 4ef7146

Please sign in to comment.