From b91fd847aabd5dde191532d1bdf619d911c6119a Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 16 Feb 2014 17:59:40 -0500 Subject: [PATCH] Update label() to use the new widgets. Replace Helper::domId() with an implementation on FormHelper. This new method simplifies the ID generation and removes the stateful entity() feature that has been problematic in the past. Now there is no shared state between method calls which will simplify the internals of helpers quite a bit. --- src/View/Helper.php | 31 ------------------- src/View/Helper/FormHelper.php | 28 +++++++++++------ tests/TestCase/View/Helper/FormHelperTest.php | 23 ++++++-------- 3 files changed, 29 insertions(+), 53 deletions(-) diff --git a/src/View/Helper.php b/src/View/Helper.php index 50e11c53f3b..c06af5940d8 100644 --- a/src/View/Helper.php +++ b/src/View/Helper.php @@ -577,37 +577,6 @@ public function field() { return $last; } -/** - * Generates a DOM ID for the selected element, if one is not set. - * Uses the current View::entity() settings to generate a CamelCased id attribute. - * - * @param array|string $options Either an array of html attributes to add $id into, or a string - * with a view entity path to get a domId for. - * @param string $id The name of the 'id' attribute. - * @return mixed If $options was an array, an array will be returned with $id set. If a string - * was supplied, a string will be returned. - */ - public function domId($options = null, $id = 'id') { - if (is_array($options) && array_key_exists($id, $options) && $options[$id] === null) { - unset($options[$id]); - return $options; - } elseif (!is_array($options) && $options !== null) { - $this->setEntity($options); - return $this->domId(); - } - - $entity = $this->entity(); - $model = array_shift($entity); - $dom = $model . implode('', array_map(array('Cake\Utility\Inflector', 'camelize'), $entity)); - - if (is_array($options) && !array_key_exists($id, $options)) { - $options[$id] = $dom; - } elseif ($options === null) { - return $dom; - } - return $options; - } - /** * Gets the input field name for the current tag. Creates input name attributes * using CakePHP's `Model[field]` formatting. diff --git a/src/View/Helper/FormHelper.php b/src/View/Helper/FormHelper.php index 549a5df9a50..be0a20e55ed 100644 --- a/src/View/Helper/FormHelper.php +++ b/src/View/Helper/FormHelper.php @@ -698,8 +698,9 @@ public function error($field, $text = null, $options = array()) { } /** - * Returns a formatted LABEL element for HTML FORMs. Will automatically generate - * a `for` attribute if one is not provided. + * Returns a formatted LABEL element for HTML forms. + * + * Will automatically generate a `for` attribute if one is not provided. * * ### Options * @@ -734,7 +735,7 @@ public function error($field, $text = null, $options = array()) { * * {{{ * echo $this->Form->label('Post.published', 'Publish', array( - * 'for' => 'post-publish' + * 'for' => 'post-publish' * )); * * }}} @@ -747,11 +748,7 @@ public function error($field, $text = null, $options = array()) { * @return string The formatted LABEL element * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::label */ - public function label($fieldName = null, $text = null, $options = array()) { - if ($fieldName === null) { - $fieldName = implode('.', $this->entity()); - } - + public function label($fieldName, $text = null, $options = array()) { if ($text === null) { if (strpos($fieldName, '.') !== false) { $fieldElements = explode('.', $fieldName); @@ -775,8 +772,21 @@ public function label($fieldName = null, $text = null, $options = array()) { } else { $labelFor = $this->domId($fieldName); } + $attrs = $options + [ + 'for' => $labelFor, + 'text' => $text, + ]; + return $this->widget('label', $attrs); + } - return $this->Html->useTag('label', $labelFor, $options, $text); +/** + * Generate an ID suitable for use in an ID attribute. + * + * @param string $value The value to convert into an ID. + * @return string The generated id. + */ + public function domId($value) { + return mb_strtolower(Inflector::slug($value, '-')); } /** diff --git a/tests/TestCase/View/Helper/FormHelperTest.php b/tests/TestCase/View/Helper/FormHelperTest.php index cb6b5fe4b68..6fa73ca25c4 100644 --- a/tests/TestCase/View/Helper/FormHelperTest.php +++ b/tests/TestCase/View/Helper/FormHelperTest.php @@ -3954,32 +3954,29 @@ public function testSelectAsCheckbox() { * @return void */ public function testLabel() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->text('Person.name'); - $result = $this->Form->label(); - $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label')); + $result = $this->Form->label('Person.name'); + $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label')); - $this->Form->text('Person.name'); - $result = $this->Form->label(); - $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label')); + $result = $this->Form->label('Person.name'); + $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label')); $result = $this->Form->label('Person.first_name'); - $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label')); + $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'First Name', '/label')); $result = $this->Form->label('Person.first_name', 'Your first name'); - $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label')); + $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'Your first name', '/label')); $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class')); - $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label')); + $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class'), 'Your first name', '/label')); $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID')); - $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label')); + $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label')); $result = $this->Form->label('Person.first_name', ''); - $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label')); + $this->assertTags($result, array('label' => array('for' => 'person-first-name'), '/label')); $result = $this->Form->label('Person.2.name', ''); - $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label')); + $this->assertTags($result, array('label' => array('for' => 'person-2-name'), '/label')); } /**