Skip to content
This repository

Added ability to define custom element options #1893

Merged
merged 8 commits into from over 1 year ago

5 participants

Zucchi Don't Add Me To Your Organization a.k.a The Travis Bot Michaël Gallego Matthew Weier O'Phinney Matt Cockayne
Zucchi
zucchi commented

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 
    ),
));
added some commits
Matt Cockayne 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
Matt Cockayne added getOptions to element 8ac7346
Don't Add Me To Your Organization a.k.a The Travis Bot

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

Don't Add Me To Your Organization a.k.a The Travis Bot

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

Don't Add Me To Your Organization a.k.a The Travis Bot

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

Don't Add Me To Your Organization a.k.a The Travis Bot

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

Don't Add Me To Your Organization a.k.a The Travis Bot

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

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request fails (merged de1d8c9 into d6b0afe).

Don't Add Me To Your Organization a.k.a The Travis Bot

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

Matthew Weier O'Phinney weierophinney commented on the diff
library/Zend/Form/Element.php
((6 lines not shown))
108 115
         return $this;
109 116
     }
110 117
 
111 118
     /**
  119
+     * get defined options
  120
+     *
  121
+     * @return array()
  122
+     */
  123
+    public function getOptions()
2
Matthew Weier O'Phinney 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
Matthew Weier O'Phinney weierophinney commented on the diff
library/Zend/Form/Element.php
((10 lines not shown))
  119
+     * get defined options
  120
+     *
  121
+     * @return array()
  122
+     */
  123
+    public function getOptions()
  124
+    {
  125
+        return $this->options;
  126
+    }
  127
+
  128
+    /**
  129
+     * return the specified option
  130
+     * 
  131
+     * @param string $option
  132
+     * @return NULL|multitype:
  133
+     */
  134
+    public function getOption($option)
2
Matthew Weier O'Phinney 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
Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged b9731fc into d6b0afe).

Michaël Gallego

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

Zucchi
zucchi commented

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

Matthew Weier O'Phinney
Owner

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

Michaël Gallego

Let's go for it, so :).

Matthew Weier O'Phinney weierophinney merged commit b9731fc into from
Matthew Weier O'Phinney weierophinney closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
32  library/Zend/Form/Element.php
@@ -39,6 +39,11 @@ class Element implements ElementInterface
39 39
      */
40 40
     protected $messages = array();
41 41
 
  42
+    /**
  43
+     * @var array custom options
  44
+     */
  45
+    protected $options = array();
  46
+
42 47
 
43 48
     /**
44 49
      * @param  null|int|string  $name    Optional name for the element
@@ -105,10 +110,37 @@ public function setOptions($options)
105 110
             $this->setLabelAttributes($options['label_attributes']);
106 111
         }
107 112
 
  113
+        $this->options = $options;
  114
+
108 115
         return $this;
109 116
     }
110 117
 
111 118
     /**
  119
+     * get defined options
  120
+     *
  121
+     * @return array()
  122
+     */
  123
+    public function getOptions()
  124
+    {
  125
+        return $this->options;
  126
+    }
  127
+
  128
+    /**
  129
+     * return the specified option
  130
+     * 
  131
+     * @param string $option
  132
+     * @return NULL|multitype:
  133
+     */
  134
+    public function getOption($option)
  135
+    {
  136
+        if (!isset($this->options[$option])) {
  137
+            return null;
  138
+        }
  139
+        
  140
+        return $this->options[$option];
  141
+    }
  142
+    
  143
+    /**
112 144
      * Set a single element attribute
113 145
      *
114 146
      * @param  string $key
15  library/Zend/Form/ElementInterface.php
@@ -41,6 +41,21 @@ public function getName();
41 41
      * @return ElementInterface
42 42
      */
43 43
     public function setOptions($options);
  44
+    
  45
+    /**
  46
+     * get the defined options
  47
+     * 
  48
+     * @return array
  49
+     */
  50
+    public function getOptions();
  51
+    
  52
+    /**
  53
+     * return the specified option
  54
+     * 
  55
+     * @param string $option
  56
+     * @return NULL|multitype
  57
+     */
  58
+    public function getOption($option);
44 59
 
45 60
     /**
46 61
      * Set a single element attribute
32  tests/Zend/Form/ElementTest.php
@@ -96,4 +96,36 @@ public function testCanPassNameToConstructor()
96 96
         $element = new Element('foo');
97 97
         $this->assertEquals('foo', $element->getName());
98 98
     }
  99
+    
  100
+    public function testCanSetCustomOptionFromConstructor()
  101
+    {
  102
+        $element = new Element('foo', array(
  103
+            'custom' => 'option'
  104
+        ));
  105
+        $options = $element->getOptions();
  106
+        $this->assertArrayHasKey('custom', $options);
  107
+        $this->assertEquals('option', $options['custom']);
  108
+    }
  109
+    
  110
+    public function testCanSetCustomOptionFromMethod()
  111
+    {
  112
+        $element = new Element('foo');
  113
+        $element->setOptions(array(
  114
+            'custom' => 'option'
  115
+        ));
  116
+        
  117
+        $options = $element->getOptions();
  118
+        $this->assertArrayHasKey('custom', $options);
  119
+        $this->assertEquals('option', $options['custom']);
  120
+    }
  121
+    
  122
+    public function testCanRetrieveSpecificOption()
  123
+    {
  124
+        $element = new Element('foo');
  125
+        $element->setOptions(array(
  126
+            'custom' => 'option'
  127
+        ));
  128
+        $option = $element->getOption('custom');
  129
+        $this->assertEquals('option', $option);
  130
+    }
99 131
 }
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.