From cd123e1ce484ba76bac5a1eb3a4dd959a18415ad Mon Sep 17 00:00:00 2001 From: Andrej Sinicyn Date: Fri, 15 Jul 2016 17:49:48 +0200 Subject: [PATCH 1/2] Adding possibility to use column-size attribute for button groups from inside of Form definitions. --- src/TwbBundle/Form/View/Helper/TwbBundleForm.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/TwbBundle/Form/View/Helper/TwbBundleForm.php b/src/TwbBundle/Form/View/Helper/TwbBundleForm.php index d77deff..2031e5f 100644 --- a/src/TwbBundle/Form/View/Helper/TwbBundleForm.php +++ b/src/TwbBundle/Form/View/Helper/TwbBundleForm.php @@ -80,6 +80,9 @@ protected function renderElements(FormInterface $oForm, $sFormLayout = self::LAY // Store button groups $aButtonGroups = array(); + // Store button groups column-size from buttons + $aButtonGroupsColumnSize = array(); + // Store elements rendering $aElementsRendering = array(); @@ -118,6 +121,10 @@ protected function renderElements(FormInterface $oForm, $sFormLayout = self::LAY } else { $aButtonGroups[$sButtonGroupKey] = array($oElement); } + if (!empty($aOptions['column-size']) && !isset($aButtonGroupsColumnSize[$sButtonGroupKey])) { + // Only the first occured column-size will be set, other are ignored. + $aButtonGroupsColumnSize[$sButtonGroupKey] = $aOptions['column-size']; + } } elseif ($oElement instanceof FieldsetInterface) { $aElementsRendering[$iKey] = $oFormCollectionHelper->__invoke($oElement); } else { @@ -133,7 +140,8 @@ protected function renderElements(FormInterface $oForm, $sFormLayout = self::LAY $aButtons = $aButtonGroups[$sElementRendering]; // Render button group content - $sFormContent .= $oFormRowHelper->renderElementFormGroup($oButtonGroupHelper($aButtons), $oFormRowHelper->getRowClassFromElement(current($aButtons))); + $options = (isset($aButtonGroupsColumnSize[$sElementRendering])) ? array('attributes' => array('class' => 'col-' . $aButtonGroupsColumnSize[$sElementRendering])) : null; + $sFormContent .= $oFormRowHelper->renderElementFormGroup($oButtonGroupHelper($aButtons, $options), $oFormRowHelper->getRowClassFromElement(current($aButtons))); } else { $sFormContent .= $sElementRendering; } From 8acd8259a93078c05f9d2a1a55ca0d4bb69b8b34 Mon Sep 17 00:00:00 2001 From: Andrej Sinicyn Date: Thu, 21 Jul 2016 08:24:14 +0200 Subject: [PATCH 2/2] Adding info about inheriting column-size attribute to a button group and a test for it. --- README.md | 19 +++++++- .../TwbBundleFormsTest.php | 46 +++++++++++++++++++ .../horizontal-form-button-group.phtml | 6 +++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 tests/_files/expected-forms/horizontal-form-button-group.phtml diff --git a/README.md b/README.md index 19e67cb..7106808 100644 --- a/README.md +++ b/README.md @@ -140,16 +140,31 @@ Here are the available layouts : The helper auto add form specific class and `form` role attributes. ### Button groups in form -Form helper can render button groups in a form row, button elements are grouped by the option `button-group`. Exemple : +Form helper can render button groups in a form row, button elements are grouped by the option `button-group`. Example : ```php add(new \Zend\Form\Element\Button('left-button', array('label' => 'Left', 'button-group' => 'group-1'))) - ->add(new \Zend\Form\Element\Button('roght-button', array('label' => 'Right', 'button-group' => 'group-1'))); + ->add(new \Zend\Form\Element\Button('right-button', array('label' => 'Right', 'button-group' => 'group-1'))); $this->form($oForm); ``` +If any button belonging to a button group has a `column-size` attribute, then the first occurrence of it will be used for the button group styling. Example : +```php +add(new \Zend\Form\Element\Button('left-button', array('label' => 'Left', 'button-group' => 'group-1', 'column-size' => 'sm-10 col-sm-offset-2'))) + ->add(new \Zend\Form\Element\Button('right-button', array('label' => 'Right', 'button-group' => 'group-1'))); +$this->form($oForm); +``` +This results in following output : +```html +
+``` + #### Button : `TwbBundle\Form\View\Helper\TwbBundleFormButton` Button helper can be called in a view with the view helper service `formButton(\Zend\Form\ElementInterface $oElement, $sButtonContent = null)` : diff --git a/tests/TwbBundleTest/BootstrapBasedTests/TwbBundleFormsTest.php b/tests/TwbBundleTest/BootstrapBasedTests/TwbBundleFormsTest.php index 9a75a6f..5ed233e 100644 --- a/tests/TwbBundleTest/BootstrapBasedTests/TwbBundleFormsTest.php +++ b/tests/TwbBundleTest/BootstrapBasedTests/TwbBundleFormsTest.php @@ -155,6 +155,52 @@ public function testHorizontalform() $this->assertStringEqualsFile($this->expectedPath . 'horizontal-form.phtml', $this->formHelper->__invoke($oForm)); } + /** + * Test http://getbootstrap.com/css/#forms-horizontal + */ + public function testHorizontalformButtonGroup() + { + $oForm = new \Zend\Form\Form(); + $oForm->add(array( + 'name' => 'input-email', + 'attributes' => array( + 'type' => 'email', + 'placeholder' => 'Enter email', + 'id' => 'inputEmail1' + ), + 'options' => array( + 'label' => 'Email', + 'column-size' => 'sm-10', + 'label_attributes' => array('class' => 'col-sm-2') + ) + ))->add(array( + 'name' => 'input-password', + 'attributes' => array( + 'type' => 'password', + 'placeholder' => 'Password', + 'id' => 'inputPassword1' + ), + 'options' => array('label' => 'Password', 'column-size' => 'sm-10', 'label_attributes' => array('class' => 'col-sm-2')) + ))->add(array( + 'name' => 'input-checkbox', + 'type' => 'checkbox', + 'options' => array('label' => 'Remember me', 'column-size' => 'sm-10 col-sm-offset-2') + ))->add(array( + 'name' => 'button-submit', + 'type' => 'button', + 'attributes' => array('type' => 'submit'), + 'options' => array('label' => 'Sign in', 'column-size' => 'sm-10 col-sm-offset-2', 'button-group' => 'group-1') + ))->add(array( + 'name' => 'button-reset', + 'type' => 'button', + 'attributes' => array('type' => 'reset'), + 'options' => array('label' => 'Reset form', 'column-size' => 'sm-8 col-sm-offset-4', 'button-group' => 'group-1') + )); + + //Test content + $this->assertStringEqualsFile($this->expectedPath . 'horizontal-form-button-group.phtml', $this->formHelper->__invoke($oForm)); + } + /** * Test http://getbootstrap.com/css/#forms-controls */ diff --git a/tests/_files/expected-forms/horizontal-form-button-group.phtml b/tests/_files/expected-forms/horizontal-form-button-group.phtml new file mode 100644 index 0000000..478b137 --- /dev/null +++ b/tests/_files/expected-forms/horizontal-form-button-group.phtml @@ -0,0 +1,6 @@ +
+
+
+
+
+
\ No newline at end of file