Browse Source

Add ID attribute prefixing feature.

ADmad 12 years ago
parent
commit
78c8b3ef70

+ 5 - 1
src/View/Helper/FormHelper.php

@@ -263,6 +263,7 @@ class FormHelper extends Helper {
  * - `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 @@ class FormHelper extends Helper {
 			'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 @@ class FormHelper extends Helper {
 
 		$this->requestType = null;
 		$this->_context = null;
+		$this->_idPrefix = null;
 		return $out;
 	}
 

+ 12 - 1
src/View/Widget/IdGeneratorTrait.php

@@ -23,6 +23,13 @@ use Cake\Utility\Inflector;
 trait IdGeneratorTrait {
 
 /**
+ * Prefix for id attribute.
+ *
+ * @var string
+ */
+	protected $_idPrefix = null;
+
+/**
  * A list of id suffixes used in the current rendering.
  *
  * @var array
@@ -67,7 +74,11 @@ trait IdGeneratorTrait {
  * @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 - 0
tests/TestCase/View/Helper/FormHelperTest.php

@@ -2014,6 +2014,49 @@ class FormHelperTest extends TestCase {
 	}
 
 /**
+ * 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.
  *
  * @return void