Skip to content
Permalink
Browse files

Add ID attribute prefixing feature.

  • Loading branch information...
ADmad committed Mar 9, 2014
1 parent ac25629 commit 78c8b3ef70f621224343a21c5e124d4f2f5cd0ef
@@ -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;
}
@@ -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;
}
}
@@ -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.
*

0 comments on commit 78c8b3e

Please sign in to comment.
You can’t perform that action at this time.