Skip to content

Commit

Permalink
Update label() to use the new widgets.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
markstory committed Feb 17, 2014
1 parent 25cb2a2 commit b91fd84
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 53 deletions.
31 changes: 0 additions & 31 deletions src/View/Helper.php
Expand Up @@ -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.
Expand Down
28 changes: 19 additions & 9 deletions src/View/Helper/FormHelper.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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'
* ));
* <label for="post-publish">Publish</label>
* }}}
Expand All @@ -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);
Expand All @@ -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, '-'));
}

/**
Expand Down
23 changes: 10 additions & 13 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -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'));
}

/**
Expand Down

0 comments on commit b91fd84

Please sign in to comment.