Skip to content

Commit b91fd84

Browse files
committed
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.
1 parent 25cb2a2 commit b91fd84

File tree

3 files changed

+29
-53
lines changed

3 files changed

+29
-53
lines changed

src/View/Helper.php

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -577,37 +577,6 @@ public function field() {
577577
return $last;
578578
}
579579

580-
/**
581-
* Generates a DOM ID for the selected element, if one is not set.
582-
* Uses the current View::entity() settings to generate a CamelCased id attribute.
583-
*
584-
* @param array|string $options Either an array of html attributes to add $id into, or a string
585-
* with a view entity path to get a domId for.
586-
* @param string $id The name of the 'id' attribute.
587-
* @return mixed If $options was an array, an array will be returned with $id set. If a string
588-
* was supplied, a string will be returned.
589-
*/
590-
public function domId($options = null, $id = 'id') {
591-
if (is_array($options) && array_key_exists($id, $options) && $options[$id] === null) {
592-
unset($options[$id]);
593-
return $options;
594-
} elseif (!is_array($options) && $options !== null) {
595-
$this->setEntity($options);
596-
return $this->domId();
597-
}
598-
599-
$entity = $this->entity();
600-
$model = array_shift($entity);
601-
$dom = $model . implode('', array_map(array('Cake\Utility\Inflector', 'camelize'), $entity));
602-
603-
if (is_array($options) && !array_key_exists($id, $options)) {
604-
$options[$id] = $dom;
605-
} elseif ($options === null) {
606-
return $dom;
607-
}
608-
return $options;
609-
}
610-
611580
/**
612581
* Gets the input field name for the current tag. Creates input name attributes
613582
* using CakePHP's `Model[field]` formatting.

src/View/Helper/FormHelper.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,9 @@ public function error($field, $text = null, $options = array()) {
698698
}
699699

700700
/**
701-
* Returns a formatted LABEL element for HTML FORMs. Will automatically generate
702-
* a `for` attribute if one is not provided.
701+
* Returns a formatted LABEL element for HTML forms.
702+
*
703+
* Will automatically generate a `for` attribute if one is not provided.
703704
*
704705
* ### Options
705706
*
@@ -734,7 +735,7 @@ public function error($field, $text = null, $options = array()) {
734735
*
735736
* {{{
736737
* echo $this->Form->label('Post.published', 'Publish', array(
737-
* 'for' => 'post-publish'
738+
* 'for' => 'post-publish'
738739
* ));
739740
* <label for="post-publish">Publish</label>
740741
* }}}
@@ -747,11 +748,7 @@ public function error($field, $text = null, $options = array()) {
747748
* @return string The formatted LABEL element
748749
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::label
749750
*/
750-
public function label($fieldName = null, $text = null, $options = array()) {
751-
if ($fieldName === null) {
752-
$fieldName = implode('.', $this->entity());
753-
}
754-
751+
public function label($fieldName, $text = null, $options = array()) {
755752
if ($text === null) {
756753
if (strpos($fieldName, '.') !== false) {
757754
$fieldElements = explode('.', $fieldName);
@@ -775,8 +772,21 @@ public function label($fieldName = null, $text = null, $options = array()) {
775772
} else {
776773
$labelFor = $this->domId($fieldName);
777774
}
775+
$attrs = $options + [
776+
'for' => $labelFor,
777+
'text' => $text,
778+
];
779+
return $this->widget('label', $attrs);
780+
}
778781

779-
return $this->Html->useTag('label', $labelFor, $options, $text);
782+
/**
783+
* Generate an ID suitable for use in an ID attribute.
784+
*
785+
* @param string $value The value to convert into an ID.
786+
* @return string The generated id.
787+
*/
788+
public function domId($value) {
789+
return mb_strtolower(Inflector::slug($value, '-'));
780790
}
781791

782792
/**

tests/TestCase/View/Helper/FormHelperTest.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3954,32 +3954,29 @@ public function testSelectAsCheckbox() {
39543954
* @return void
39553955
*/
39563956
public function testLabel() {
3957-
$this->markTestIncomplete('Need to revisit once models work again.');
3958-
$this->Form->text('Person.name');
3959-
$result = $this->Form->label();
3960-
$this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
3957+
$result = $this->Form->label('Person.name');
3958+
$this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
39613959

3962-
$this->Form->text('Person.name');
3963-
$result = $this->Form->label();
3964-
$this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
3960+
$result = $this->Form->label('Person.name');
3961+
$this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
39653962

39663963
$result = $this->Form->label('Person.first_name');
3967-
$this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
3964+
$this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'First Name', '/label'));
39683965

39693966
$result = $this->Form->label('Person.first_name', 'Your first name');
3970-
$this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
3967+
$this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'Your first name', '/label'));
39713968

39723969
$result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
3973-
$this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
3970+
$this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class'), 'Your first name', '/label'));
39743971

39753972
$result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
3976-
$this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
3973+
$this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
39773974

39783975
$result = $this->Form->label('Person.first_name', '');
3979-
$this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
3976+
$this->assertTags($result, array('label' => array('for' => 'person-first-name'), '/label'));
39803977

39813978
$result = $this->Form->label('Person.2.name', '');
3982-
$this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
3979+
$this->assertTags($result, array('label' => array('for' => 'person-2-name'), '/label'));
39833980
}
39843981

39853982
/**

0 commit comments

Comments
 (0)