Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added ability to define custom element options #1893

Merged
merged 8 commits into from

5 participants

@zucchi

added $options property with appropriate getters and setters to allow for adding of custom options to an element.

This allows the user to then add additional data that can then be utilised in other ways and makes form elemets much more flexible for personal implementations

e.g. $this->add(array(
    'name'  => 'name',
    'attributes' => array(
        'type' => 'text',
    ),
    'options' => array(
        'label' => 'Name',
        'description' => 'The name of the domain', // not currently handled 
    ),
));
mattcockayne added some commits
@mattcockayne mattcockayne Added storage for custom element options
added $options property with appropriate getters and setters
to allow for adding of custom options to an element.
0116a92
@mattcockayne mattcockayne added getOptions to element 8ac7346
@travisbot

This pull request fails (merged 0116a92 into d6b0afe).

@travisbot

This pull request fails (merged 8ac7346 into d6b0afe).

@travisbot

This pull request fails (merged 5e847c2 into d6b0afe).

@travisbot

This pull request passes (merged 38f2296 into d6b0afe).

@travisbot

This pull request passes (merged 0b69f2d into d6b0afe).

@travisbot

This pull request fails (merged de1d8c9 into d6b0afe).

@travisbot

This pull request passes (merged 4c8ffb7 into d6b0afe).

@weierophinney weierophinney commented on the diff
library/Zend/Form/Element.php
((6 lines not shown))
return $this;
}
/**
+ * get defined options
+ *
+ * @return array()
+ */
+ public function getOptions()
@weierophinney Owner

This method should be added to ElementInterface.

@zucchi
zucchi added a note

have added to ElementInterface

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney weierophinney commented on the diff
library/Zend/Form/Element.php
((10 lines not shown))
+ * get defined options
+ *
+ * @return array()
+ */
+ public function getOptions()
+ {
+ return $this->options;
+ }
+
+ /**
+ * return the specified option
+ *
+ * @param string $option
+ * @return NULL|multitype:
+ */
+ public function getOption($option)
@weierophinney Owner

This method should be added to ElementInterface.

@zucchi
zucchi added a note

have added to ElementInterface

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@travisbot

This pull request passes (merged b9731fc into d6b0afe).

@bakura10

When this would be useful exactly ? Why not creating a new element if you need specific options ?

@zucchi

The main reason I implemented this change is due to some view helpers I'm creating to allow easier rendering using twitters bootstrap.

https://github.com/zucchi/Zucchi-Framework/tree/master/src/Zucchi/Form/View/Helper

I shouldn't have to create a whole new element to add some prettyness.

This then allows me to add some data programatically to my element that can then be used elsewhere.

This results in me being able to define an element such as

    $this->add(array(
        'name'  => 'price',
        'attributes' => array(
            'type' => 'text',
            'required' => 'required',
            'placeholder' => '9.99',
        ),
        'options' => array(
            'label' => 'Price',
            'bootstrap' => array( // options for bootstrap form
                'help' => array(
                    'style' => 'block',
                    'content' => 'The price of the product',
                ),
                'prepend' => array('$'),
                'append => array('c'),
            )
        ),
    ));

which can then be consumed by my view helpers to generate complex bootstrap style forms.

I'm pretty sure that there will be other use cases.

My main concern is that without having the ability to define arbitrary "options" we are then closing off the way that the elements work and can be used/manipulated, meaning it is not easy to do some pretty straight forward things without having to subclass tons of elements.

Once this PR is accepted I'll be putting out a blog about it to go into more detail

@weierophinney

@bakura10 I think it's reasonable, particularly considering the "description" use case.

@bakura10

Let's go for it, so :).

@weierophinney weierophinney merged commit b9731fc into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 15, 2012
  1. @mattcockayne

    Added storage for custom element options

    mattcockayne authored
    added $options property with appropriate getters and setters
    to allow for adding of custom options to an element.
  2. @mattcockayne
Commits on Jul 16, 2012
  1. @mattcockayne
  2. @mattcockayne
  3. @mattcockayne
  4. @mattcockayne
  5. @mattcockayne

    corrected typo in tests

    mattcockayne authored
  6. @mattcockayne
This page is out of date. Refresh to see the latest.
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 Owner

This method should be added to ElementInterface.

@zucchi
zucchi added a note

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 Owner

This method should be added to ElementInterface.

@zucchi
zucchi added a note

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);
+ }
}
Something went wrong with that request. Please try again.