Added ability to define custom element options #1893

Merged
merged 8 commits into from Jul 20, 2012

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 
    ),
));
phpboyscout added some commits Jul 15, 2012
@phpboyscout phpboyscout 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
@phpboyscout phpboyscout 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 Jul 16, 2012
library/Zend/Form/Element.php
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
@weierophinney weierophinney commented on the diff Jul 16, 2012
library/Zend/Form/Element.php
+ * 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
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
@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
Zend Framework member

@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 zendframework:master Jul 20, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment