diff --git a/src/View/Helper/FormHelper.php b/src/View/Helper/FormHelper.php index 4376c524f3a..211203059c2 100755 --- a/src/View/Helper/FormHelper.php +++ b/src/View/Helper/FormHelper.php @@ -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 @@ -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'])) { @@ -429,6 +432,7 @@ public function end($secureAttributes = []) { $this->requestType = null; $this->_context = null; + $this->_idPrefix = null; return $out; } diff --git a/src/View/Widget/IdGeneratorTrait.php b/src/View/Widget/IdGeneratorTrait.php index 2b6e34b33c4..34c23c56409 100644 --- a/src/View/Widget/IdGeneratorTrait.php +++ b/src/View/Widget/IdGeneratorTrait.php @@ -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. * @@ -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; } } diff --git a/tests/TestCase/View/Helper/FormHelperTest.php b/tests/TestCase/View/Helper/FormHelperTest.php index 85f153f0ffa..4844d19a054 100755 --- a/tests/TestCase/View/Helper/FormHelperTest.php +++ b/tests/TestCase/View/Helper/FormHelperTest.php @@ -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. *