Skip to content

Commit 78c8b3e

Browse files
committed
Add ID attribute prefixing feature.
1 parent ac25629 commit 78c8b3e

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

src/View/Helper/FormHelper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ protected function _isRequiredField($validationRules) {
263263
* - `encoding` Set the accept-charset encoding for the form. Defaults to `Configure::read('App.encoding')`
264264
* - `context` Additional options for the context class. For example the EntityContext accepts a 'table'
265265
* option that allows you to set the specific Table class the form should be based on.
266+
* - `idPrefix` Prefix for generated ID attributes.
266267
*
267268
* @param mixed $model The context for which the form is being defined. Can
268269
* be an ORM entity, ORM resultset, or an array of meta data. You can use false or null
@@ -289,10 +290,12 @@ public function create($model = null, $options = []) {
289290
'url' => null,
290291
'default' => true,
291292
'encoding' => strtolower(Configure::read('App.encoding')),
293+
'idPrefix' => null
292294
];
293295

296+
$this->_idPrefix = $options['idPrefix'];
294297
$action = $this->url($this->_formUrl($context, $options));
295-
unset($options['url'], $options['action']);
298+
unset($options['url'], $options['action'], $options['idPrefix']);
296299

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

430433
$this->requestType = null;
431434
$this->_context = null;
435+
$this->_idPrefix = null;
432436
return $out;
433437
}
434438

src/View/Widget/IdGeneratorTrait.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
*/
2323
trait IdGeneratorTrait {
2424

25+
/**
26+
* Prefix for id attribute.
27+
*
28+
* @var string
29+
*/
30+
protected $_idPrefix = null;
31+
2532
/**
2633
* A list of id suffixes used in the current rendering.
2734
*
@@ -67,7 +74,11 @@ protected function _id($name, $val) {
6774
* @return string The generated id.
6875
*/
6976
protected function _domId($value) {
70-
return mb_strtolower(Inflector::slug($value, '-'));
77+
$domId = mb_strtolower(Inflector::slug($value, '-'));
78+
if (!empty($this->_idPrefix)) {
79+
$domId = $this->_idPrefix . '-' . $domId;
80+
}
81+
return $domId;
7182
}
7283

7384
}

tests/TestCase/View/Helper/FormHelperTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,6 +2013,49 @@ public function testInputCustomization() {
20132013
$this->assertTags($result, $expected);
20142014
}
20152015

2016+
/**
2017+
* Test id prefix
2018+
*
2019+
* @return void
2020+
*/
2021+
public function testCreateIdPrefix() {
2022+
$this->Form->create(false, array('idPrefix' => 'prefix'));
2023+
2024+
$result = $this->Form->input('field');
2025+
$expected = array(
2026+
'div' => array('class' => 'input text'),
2027+
'label' => array('for' => 'prefix-field'),
2028+
'Field',
2029+
'/label',
2030+
'input' => array('type' => 'text', 'name' => 'field', 'id' => 'prefix-field'),
2031+
'/div'
2032+
);
2033+
$this->assertTags($result, $expected);
2034+
2035+
$result = $this->Form->input('field', ['id' => 'custom-id']);
2036+
$expected = array(
2037+
'div' => array('class' => 'input text'),
2038+
'label' => array('for' => 'custom-id'),
2039+
'Field',
2040+
'/label',
2041+
'input' => array('type' => 'text', 'name' => 'field', 'id' => 'custom-id'),
2042+
'/div'
2043+
);
2044+
$this->assertTags($result, $expected);
2045+
2046+
$this->Form->end();
2047+
$result = $this->Form->input('field');
2048+
$expected = array(
2049+
'div' => array('class' => 'input text'),
2050+
'label' => array('for' => 'field'),
2051+
'Field',
2052+
'/label',
2053+
'input' => array('type' => 'text', 'name' => 'field', 'id' => 'field'),
2054+
'/div'
2055+
);
2056+
$this->assertTags($result, $expected);
2057+
}
2058+
20162059
/**
20172060
* Test that inputs with 0 can be created.
20182061
*

0 commit comments

Comments
 (0)