Added ability to define custom element options #1893

Merged
merged 8 commits into from Jul 20, 2012
View
32 library/Zend/Form/Element.php
@@ -39,6 +39,11 @@ class Element implements ElementInterface
*/
protected $messages = array();
+ /**
+ * @var array custom options
+ */
+ protected $options = array();
+
/**
* @param null|int|string $name Optional name for the element
@@ -105,10 +110,37 @@ public function setOptions($options)
$this->setLabelAttributes($options['label_attributes']);
}
+ $this->options = $options;
+
return $this;
}
/**
+ * get defined options
+ *
+ * @return array()
+ */
+ public function getOptions()
@weierophinney
Zend Framework member

This method should be added to ElementInterface.

@zucchi
zucchi added a note Jul 16, 2012

have added to ElementInterface

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {
+ return $this->options;
+ }
+
+ /**
+ * return the specified option
+ *
+ * @param string $option
+ * @return NULL|multitype:
+ */
+ public function getOption($option)
@weierophinney
Zend Framework member

This method should be added to ElementInterface.

@zucchi
zucchi added a note Jul 16, 2012

have added to ElementInterface

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {
+ if (!isset($this->options[$option])) {
+ return null;
+ }
+
+ return $this->options[$option];
+ }
+
+ /**
* Set a single element attribute
*
* @param string $key
View
15 library/Zend/Form/ElementInterface.php
@@ -41,6 +41,21 @@ public function getName();
* @return ElementInterface
*/
public function setOptions($options);
+
+ /**
+ * get the defined options
+ *
+ * @return array
+ */
+ public function getOptions();
+
+ /**
+ * return the specified option
+ *
+ * @param string $option
+ * @return NULL|multitype
+ */
+ public function getOption($option);
/**
* Set a single element attribute
View
32 tests/Zend/Form/ElementTest.php
@@ -96,4 +96,36 @@ public function testCanPassNameToConstructor()
$element = new Element('foo');
$this->assertEquals('foo', $element->getName());
}
+
+ public function testCanSetCustomOptionFromConstructor()
+ {
+ $element = new Element('foo', array(
+ 'custom' => 'option'
+ ));
+ $options = $element->getOptions();
+ $this->assertArrayHasKey('custom', $options);
+ $this->assertEquals('option', $options['custom']);
+ }
+
+ public function testCanSetCustomOptionFromMethod()
+ {
+ $element = new Element('foo');
+ $element->setOptions(array(
+ 'custom' => 'option'
+ ));
+
+ $options = $element->getOptions();
+ $this->assertArrayHasKey('custom', $options);
+ $this->assertEquals('option', $options['custom']);
+ }
+
+ public function testCanRetrieveSpecificOption()
+ {
+ $element = new Element('foo');
+ $element->setOptions(array(
+ 'custom' => 'option'
+ ));
+ $option = $element->getOption('custom');
+ $this->assertEquals('option', $option);
+ }
}