Skip to content

Commit

Permalink
Add ID attribute prefixing feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Mar 9, 2014
1 parent ac25629 commit 78c8b3e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/View/Helper/FormHelper.php
Expand Up @@ -263,6 +263,7 @@ protected function _isRequiredField($validationRules) {
* - `encoding` Set the accept-charset encoding for the form. Defaults to `Configure::read('App.encoding')`
* - `context` Additional options for the context class. For example the EntityContext accepts a 'table'
* option that allows you to set the specific Table class the form should be based on.
* - `idPrefix` Prefix for generated ID attributes.
*
* @param mixed $model The context for which the form is being defined. Can
* be an ORM entity, ORM resultset, or an array of meta data. You can use false or null
Expand All @@ -289,10 +290,12 @@ public function create($model = null, $options = []) {
'url' => null,
'default' => true,
'encoding' => strtolower(Configure::read('App.encoding')),
'idPrefix' => null
];

$this->_idPrefix = $options['idPrefix'];
$action = $this->url($this->_formUrl($context, $options));
unset($options['url'], $options['action']);
unset($options['url'], $options['action'], $options['idPrefix']);

$htmlAttributes = [];
switch (strtolower($options['type'])) {
Expand Down Expand Up @@ -429,6 +432,7 @@ public function end($secureAttributes = []) {

$this->requestType = null;
$this->_context = null;
$this->_idPrefix = null;
return $out;
}

Expand Down
13 changes: 12 additions & 1 deletion src/View/Widget/IdGeneratorTrait.php
Expand Up @@ -22,6 +22,13 @@
*/
trait IdGeneratorTrait {

/**
* Prefix for id attribute.
*
* @var string
*/
protected $_idPrefix = null;

/**
* A list of id suffixes used in the current rendering.
*
Expand Down Expand Up @@ -67,7 +74,11 @@ protected function _id($name, $val) {
* @return string The generated id.
*/
protected function _domId($value) {
return mb_strtolower(Inflector::slug($value, '-'));
$domId = mb_strtolower(Inflector::slug($value, '-'));
if (!empty($this->_idPrefix)) {
$domId = $this->_idPrefix . '-' . $domId;
}
return $domId;
}

}
43 changes: 43 additions & 0 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -2013,6 +2013,49 @@ public function testInputCustomization() {
$this->assertTags($result, $expected);
}

/**
* Test id prefix
*
* @return void
*/
public function testCreateIdPrefix() {
$this->Form->create(false, array('idPrefix' => 'prefix'));

$result = $this->Form->input('field');
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'prefix-field'),
'Field',
'/label',
'input' => array('type' => 'text', 'name' => 'field', 'id' => 'prefix-field'),
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->input('field', ['id' => 'custom-id']);
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'custom-id'),
'Field',
'/label',
'input' => array('type' => 'text', 'name' => 'field', 'id' => 'custom-id'),
'/div'
);
$this->assertTags($result, $expected);

$this->Form->end();
$result = $this->Form->input('field');
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'field'),
'Field',
'/label',
'input' => array('type' => 'text', 'name' => 'field', 'id' => 'field'),
'/div'
);
$this->assertTags($result, $expected);
}

/**
* Test that inputs with 0 can be created.
*
Expand Down

0 comments on commit 78c8b3e

Please sign in to comment.