From a0fe331545e22d77064920a1c28e555b67982327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9=20=D0=A7=D0=B5=D1=88?= =?UTF-8?q?=D1=83=D0=B8=D0=BD?= Date: Tue, 19 Nov 2019 10:30:58 +0700 Subject: [PATCH 1/2] Custom attributes to 'form', 'fieldset' and 'form-group' wapper Allow using of custom attributes in 'form', 'fieldset' tags. Also added ability to add and use custom attributes to form groups. --- config/module.config.php | 41 ++++++++++-------- .../Helper/Factory/FormCollectionFactory.php | 43 +++++++++++++++++++ .../Form/View/Helper/Factory/FormFactory.php | 43 +++++++++++++++++++ .../View/Helper/Factory/FormRowFactory.php | 43 +++++++++++++++++++ src/TwbsHelper/Form/View/Helper/Form.php | 28 ++++++++++++ .../Form/View/Helper/FormCollection.php | 29 +++++++++++++ src/TwbsHelper/Form/View/Helper/FormRow.php | 33 ++++++++++++++ 7 files changed, 241 insertions(+), 19 deletions(-) create mode 100644 src/TwbsHelper/Form/View/Helper/Factory/FormCollectionFactory.php create mode 100644 src/TwbsHelper/Form/View/Helper/Factory/FormFactory.php create mode 100644 src/TwbsHelper/Form/View/Helper/Factory/FormRowFactory.php diff --git a/config/module.config.php b/config/module.config.php index d92b1ea64..8ec24dae7 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -4,7 +4,7 @@ return [ 'twbshelper' => [ - 'ignoredViewHelpers' => [ + 'ignoredViewHelpers' => [ 'button', 'checkbox', 'file', @@ -16,20 +16,20 @@ 'static', 'add_on', ], - 'validTagAttributes' => [], + 'validTagAttributes' => [], 'validTagAttributePrefixes' => [], - 'type_map' => [], - 'class_map' => [], + 'type_map' => [], + 'class_map' => [], ], 'service_manager' => [ 'factories' => [ - \TwbsHelper\Options\ModuleOptions::class => \TwbsHelper\Options\Factory\ModuleOptionsFactory::class, + \TwbsHelper\Options\ModuleOptions::class => \TwbsHelper\Options\Factory\ModuleOptionsFactory::class, \Zend\I18n\Translator\TranslatorInterface::class => \Zend\I18n\Translator\TranslatorServiceFactory::class, ], ], - 'view_helpers' => [ + 'view_helpers' => [ 'invokables' => [ // Misc view helpers 'abbreviation' => \TwbsHelper\View\Helper\Abbreviation::class, @@ -60,17 +60,14 @@ 'toast' => \TwbsHelper\View\Helper\Toast::class, // Form view helpers - 'form' => \TwbsHelper\Form\View\Helper\Form::class, 'formAddOn' => \TwbsHelper\Form\View\Helper\FormAddOn::class, 'formButton' => \TwbsHelper\Form\View\Helper\FormButton::class, 'formSubmit' => \TwbsHelper\Form\View\Helper\FormButton::class, 'formCheckbox' => \TwbsHelper\Form\View\Helper\FormCheckbox::class, - 'formCollection' => \TwbsHelper\Form\View\Helper\FormCollection::class, 'formElementErrors' => \TwbsHelper\Form\View\Helper\FormElementErrors::class, 'formMultiCheckbox' => \TwbsHelper\Form\View\Helper\FormMultiCheckbox::class, 'formRadio' => \TwbsHelper\Form\View\Helper\FormRadio::class, 'formRange' => \TwbsHelper\Form\View\Helper\FormRange::class, - 'formRow' => \TwbsHelper\Form\View\Helper\FormRow::class, 'formSelect' => \TwbsHelper\Form\View\Helper\FormSelect::class, 'formErrors' => \TwbsHelper\Form\View\Helper\FormErrors::class, 'formLabel' => \TwbsHelper\Form\View\Helper\FormLabel::class, @@ -80,24 +77,20 @@ 'form_button' => \TwbsHelper\Form\View\Helper\FormButton::class, 'form_submit' => \TwbsHelper\Form\View\Helper\FormButton::class, 'form_checkbox' => \TwbsHelper\Form\View\Helper\FormCheckbox::class, - 'form_collection' => \TwbsHelper\Form\View\Helper\FormCollection::class, 'form_element_errors' => \TwbsHelper\Form\View\Helper\FormElementErrors::class, 'form_label' => \TwbsHelper\Form\View\Helper\FormLabel::class, 'form_multi_checkbox' => \TwbsHelper\Form\View\Helper\FormMultiCheckbox::class, 'form_radio' => \TwbsHelper\Form\View\Helper\FormRadio::class, - 'form_row' => \TwbsHelper\Form\View\Helper\FormRow::class, 'form_errors' => \TwbsHelper\Form\View\Helper\FormErrors::class, 'formaddon' => \TwbsHelper\Form\View\Helper\FormAddOn::class, 'formbutton' => \TwbsHelper\Form\View\Helper\FormButton::class, 'formsubmit' => \TwbsHelper\Form\View\Helper\FormButton::class, 'formcheckbox' => \TwbsHelper\Form\View\Helper\FormCheckbox::class, - 'formcollection' => \TwbsHelper\Form\View\Helper\FormCollection::class, 'formelement_errors' => \TwbsHelper\Form\View\Helper\FormElementErrors::class, 'formfile' => \TwbsHelper\Form\View\Helper\FormFile::class, 'formmulticheckbox' => \TwbsHelper\Form\View\Helper\FormMultiCheckbox::class, 'formradio' => \TwbsHelper\Form\View\Helper\FormRadio::class, 'formrange' => \TwbsHelper\Form\View\Helper\FormRange::class, - 'formrow' => \TwbsHelper\Form\View\Helper\FormRow::class, 'formselect' => \TwbsHelper\Form\View\Helper\FormSelect::class, 'formerrors' => \TwbsHelper\Form\View\Helper\FormErrors::class, 'formlabel' => \TwbsHelper\Form\View\Helper\FormLabel::class, @@ -109,18 +102,28 @@ 'formtextarea' => \Zend\Form\View\Helper\FormTextarea::class, 'forminput' => \Zend\Form\View\Helper\FormInput::class, 'formhidden' => \Zend\Form\View\Helper\FormHidden::class, - 'formsearch' => \Zend\Form\View\Helper\FormSearch::class, + 'formsearch' => \Zend\Form\View\Helper\FormSearch::class, 'translate' => \Zend\I18n\View\Helper\Translate::class, ], 'factories' => [ - \TwbsHelper\Form\View\Helper\FormElement::class => \TwbsHelper\Form\View\Helper\Factory\FormElementFactory::class, + \TwbsHelper\Form\View\Helper\Form::class => \TwbsHelper\Form\View\Helper\Factory\FormFactory::class, + \TwbsHelper\Form\View\Helper\FormElement::class => \TwbsHelper\Form\View\Helper\Factory\FormElementFactory::class, + \TwbsHelper\Form\View\Helper\FormRow::class => \TwbsHelper\Form\View\Helper\Factory\FormRowFactory::class, + \TwbsHelper\Form\View\Helper\FormCollection::class => \TwbsHelper\Form\View\Helper\Factory\FormCollectionFactory::class, ], 'aliases' => [ - 'formElement' => \TwbsHelper\Form\View\Helper\FormElement::class, - 'form_element' => \TwbsHelper\Form\View\Helper\FormElement::class, - 'formelement' => \TwbsHelper\Form\View\Helper\FormElement::class, + 'formElement' => \TwbsHelper\Form\View\Helper\FormElement::class, + 'form_element' => \TwbsHelper\Form\View\Helper\FormElement::class, + 'formelement' => \TwbsHelper\Form\View\Helper\FormElement::class, + 'form' => \TwbsHelper\Form\View\Helper\Form::class, + 'formCollection' => \TwbsHelper\Form\View\Helper\FormCollection::class, + 'form_collection' => \TwbsHelper\Form\View\Helper\FormCollection::class, + 'formcollection' => \TwbsHelper\Form\View\Helper\FormCollection::class, + 'formRow' => \TwbsHelper\Form\View\Helper\FormRow::class, + 'form_row' => \TwbsHelper\Form\View\Helper\FormRow::class, + 'formrow' => \TwbsHelper\Form\View\Helper\FormRow::class, ], ], 'navigation_helpers' => [ @@ -134,7 +137,7 @@ 'zendviewhelpernavigationnavbar' => \TwbsHelper\View\Helper\Navigation\Navbar::class, ], ], - 'view_manager' => [ + 'view_manager' => [ 'template_map' => [ __NAMESPACE__ . '/pagination_control' => __DIR__ . '/../view/partial/twbsPaginationControl.phtml', ], diff --git a/src/TwbsHelper/Form/View/Helper/Factory/FormCollectionFactory.php b/src/TwbsHelper/Form/View/Helper/Factory/FormCollectionFactory.php new file mode 100644 index 000000000..afd1836f6 --- /dev/null +++ b/src/TwbsHelper/Form/View/Helper/Factory/FormCollectionFactory.php @@ -0,0 +1,43 @@ += 2.2) -> proxy to __invoke + * + * @param \Zend\ServiceManager\ServiceLocatorInterface $oServiceLocator + * @param mixed $sCanonicalName + * @param mixed $sRequestedName + * @return void + */ + public function createService( + \Zend\ServiceManager\ServiceLocatorInterface $oServiceLocator, + $sCanonicalName = null, + $sRequestedName = null + ) { + return $this($oServiceLocator, $sRequestedName); + } + + /** + * Compatibility with ZF3 + * + * @param \Interop\Container\ContainerInterface $oContainer + * @param mixed $sRequestedName + * @param array $aOptions + * @return void + */ + public function __invoke(\Interop\Container\ContainerInterface $oContainer, $sRequestedName, array $aOptions = null) + { + $oOptions = $oContainer->get(\TwbsHelper\Options\ModuleOptions::class); + + return new \TwbsHelper\Form\View\Helper\FormCollection($oOptions); + } +} \ No newline at end of file diff --git a/src/TwbsHelper/Form/View/Helper/Factory/FormFactory.php b/src/TwbsHelper/Form/View/Helper/Factory/FormFactory.php new file mode 100644 index 000000000..e67736eae --- /dev/null +++ b/src/TwbsHelper/Form/View/Helper/Factory/FormFactory.php @@ -0,0 +1,43 @@ += 2.2) -> proxy to __invoke + * + * @param \Zend\ServiceManager\ServiceLocatorInterface $oServiceLocator + * @param mixed $sCanonicalName + * @param mixed $sRequestedName + * @return void + */ + public function createService( + \Zend\ServiceManager\ServiceLocatorInterface $oServiceLocator, + $sCanonicalName = null, + $sRequestedName = null + ) { + return $this($oServiceLocator, $sRequestedName); + } + + /** + * Compatibility with ZF3 + * + * @param \Interop\Container\ContainerInterface $oContainer + * @param mixed $sRequestedName + * @param array $aOptions + * @return void + */ + public function __invoke(\Interop\Container\ContainerInterface $oContainer, $sRequestedName, array $aOptions = null) + { + $oOptions = $oContainer->get(\TwbsHelper\Options\ModuleOptions::class); + + return new \TwbsHelper\Form\View\Helper\Form($oOptions); + } +} \ No newline at end of file diff --git a/src/TwbsHelper/Form/View/Helper/Factory/FormRowFactory.php b/src/TwbsHelper/Form/View/Helper/Factory/FormRowFactory.php new file mode 100644 index 000000000..d5107bb17 --- /dev/null +++ b/src/TwbsHelper/Form/View/Helper/Factory/FormRowFactory.php @@ -0,0 +1,43 @@ += 2.2) -> proxy to __invoke + * + * @param \Zend\ServiceManager\ServiceLocatorInterface $oServiceLocator + * @param mixed $sCanonicalName + * @param mixed $sRequestedName + * @return void + */ + public function createService( + \Zend\ServiceManager\ServiceLocatorInterface $oServiceLocator, + $sCanonicalName = null, + $sRequestedName = null + ) { + return $this($oServiceLocator, $sRequestedName); + } + + /** + * Compatibility with ZF3 + * + * @param \Interop\Container\ContainerInterface $oContainer + * @param mixed $sRequestedName + * @param array $aOptions + * @return void + */ + public function __invoke(\Interop\Container\ContainerInterface $oContainer, $sRequestedName, array $aOptions = null) + { + $oOptions = $oContainer->get(\TwbsHelper\Options\ModuleOptions::class); + + return new \TwbsHelper\Form\View\Helper\FormRow($oOptions); + } +} \ No newline at end of file diff --git a/src/TwbsHelper/Form/View/Helper/Form.php b/src/TwbsHelper/Form/View/Helper/Form.php index 6cedb7789..1eea89043 100644 --- a/src/TwbsHelper/Form/View/Helper/Form.php +++ b/src/TwbsHelper/Form/View/Helper/Form.php @@ -9,6 +9,21 @@ class Form extends \Zend\Form\View\Helper\Form const LAYOUT_HORIZONTAL = 'horizontal'; const LAYOUT_INLINE = 'inline'; + // Hold configurable options + protected $options; + + /** + * Constructor + * + * @param \TwbsHelper\Options\ModuleOptions $options + * @access public + * @return void + */ + public function __construct(\TwbsHelper\Options\ModuleOptions $options) + { + $this->options = $options; + } + /** * @param \Zend\Form\FormInterface $oForm * @return \TwbsHelper\Form\View\Helper\Form|string @@ -29,6 +44,19 @@ public function __invoke(\Zend\Form\FormInterface $oForm = null) */ public function render(\Zend\Form\FormInterface $oForm): string { + // Add valid custom attributes + if ($this->options->getValidTagAttributes()) { + foreach ($this->options->getValidTagAttributes() as $attribute) { + $this->addValidAttribute($attribute); + } + } + + if ($this->options->getValidTagAttributePrefixes()) { + foreach ($this->options->getValidTagAttributePrefixes() as $prefix) { + $this->addValidAttributePrefix($prefix); + } + } + // Prepare form if needed if (method_exists($oForm, 'prepare')) { $oForm->prepare(); diff --git a/src/TwbsHelper/Form/View/Helper/FormCollection.php b/src/TwbsHelper/Form/View/Helper/FormCollection.php index b4bbf6ca6..28b3a4277 100644 --- a/src/TwbsHelper/Form/View/Helper/FormCollection.php +++ b/src/TwbsHelper/Form/View/Helper/FormCollection.php @@ -12,6 +12,9 @@ class FormCollection extends \Zend\Form\View\Helper\FormCollection protected static $fieldsetRegex = '/(]*>)([\s\S]*)(<\/fieldset[^>]*>)$/imU'; protected static $legendRegex = '/]*>([\s\S]*)<\/legend[^>]*>/imU'; + // Hold configurable options + protected $options; + /** * This is the default wrapper that the collection is wrapped into * @@ -19,6 +22,19 @@ class FormCollection extends \Zend\Form\View\Helper\FormCollection */ protected $wrapper = '%2$s%1$s%3$s'; + + /** + * Constructor + * + * @param \TwbsHelper\Options\ModuleOptions $options + * @access public + * @return void + */ + public function __construct(\TwbsHelper\Options\ModuleOptions $options) + { + $this->options = $options; + } + /** * Render a collection by iterating through all fieldsets and elements * @@ -27,6 +43,19 @@ class FormCollection extends \Zend\Form\View\Helper\FormCollection */ public function render(\Zend\Form\ElementInterface $oElement): string { + // Add valid custom attributes + if ($this->options->getValidTagAttributes()) { + foreach ($this->options->getValidTagAttributes() as $attribute) { + $this->addValidAttribute($attribute); + } + } + + if ($this->options->getValidTagAttributePrefixes()) { + foreach ($this->options->getValidTagAttributePrefixes() as $prefix) { + $this->addValidAttributePrefix($prefix); + } + } + $sElementLayout = $oElement->getOption('layout'); // Set form layout class diff --git a/src/TwbsHelper/Form/View/Helper/FormRow.php b/src/TwbsHelper/Form/View/Helper/FormRow.php index 09714ba42..5824f5f78 100644 --- a/src/TwbsHelper/Form/View/Helper/FormRow.php +++ b/src/TwbsHelper/Form/View/Helper/FormRow.php @@ -13,6 +13,21 @@ class FormRow extends \Zend\Form\View\Helper\FormRow */ protected $inputErrorClass = 'is-invalid'; + // Hold configurable options + protected $options; + + /** + * Constructor + * + * @param \TwbsHelper\Options\ModuleOptions $options + * @access public + * @return void + */ + public function __construct(\TwbsHelper\Options\ModuleOptions $options) + { + $this->options = $options; + } + /** * @param \Zend\Form\ElementInterface $oElement * @param string|null $sLabelPosition @@ -125,6 +140,24 @@ public function renderFormRow(\Zend\Form\ElementInterface $oElement, $sElementCo ); } + // Add valid custom attributes + if ($this->options->getValidTagAttributes()) { + foreach ($this->options->getValidTagAttributes() as $attribute) { + $this->addValidAttribute($attribute); + } + } + + if ($this->options->getValidTagAttributePrefixes()) { + foreach ($this->options->getValidTagAttributePrefixes() as $prefix) { + $this->addValidAttributePrefix($prefix); + } + } + + // Additional row attributes + if ($aRowAdditionalAttributes = $oElement->getOption('row-attributes')) { + $aAttributes = array_merge($aAttributes, $aRowAdditionalAttributes); + } + // Render element into form group return $this->htmlElement( 'div', From ea8e0799962d52721bf53e0bf76fc8d331e10585 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9=20=D0=A7=D0=B5=D1=88?= =?UTF-8?q?=D1=83=D0=B8=D0=BD?= Date: Tue, 19 Nov 2019 15:45:40 +0700 Subject: [PATCH 2/2] Fixed lint issues --- .../Form/View/Helper/Factory/FormCollectionFactory.php | 3 +-- src/TwbsHelper/Form/View/Helper/Factory/FormFactory.php | 3 +-- src/TwbsHelper/Form/View/Helper/Factory/FormRowFactory.php | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/TwbsHelper/Form/View/Helper/Factory/FormCollectionFactory.php b/src/TwbsHelper/Form/View/Helper/Factory/FormCollectionFactory.php index afd1836f6..c0499cd77 100644 --- a/src/TwbsHelper/Form/View/Helper/Factory/FormCollectionFactory.php +++ b/src/TwbsHelper/Form/View/Helper/Factory/FormCollectionFactory.php @@ -7,7 +7,6 @@ namespace TwbsHelper\Form\View\Helper\Factory; - class FormCollectionFactory implements \Zend\ServiceManager\FactoryInterface { /** @@ -40,4 +39,4 @@ public function __invoke(\Interop\Container\ContainerInterface $oContainer, $sRe return new \TwbsHelper\Form\View\Helper\FormCollection($oOptions); } -} \ No newline at end of file +} diff --git a/src/TwbsHelper/Form/View/Helper/Factory/FormFactory.php b/src/TwbsHelper/Form/View/Helper/Factory/FormFactory.php index e67736eae..981c97a54 100644 --- a/src/TwbsHelper/Form/View/Helper/Factory/FormFactory.php +++ b/src/TwbsHelper/Form/View/Helper/Factory/FormFactory.php @@ -7,7 +7,6 @@ namespace TwbsHelper\Form\View\Helper\Factory; - class FormFactory implements \Zend\ServiceManager\FactoryInterface { /** @@ -40,4 +39,4 @@ public function __invoke(\Interop\Container\ContainerInterface $oContainer, $sRe return new \TwbsHelper\Form\View\Helper\Form($oOptions); } -} \ No newline at end of file +} diff --git a/src/TwbsHelper/Form/View/Helper/Factory/FormRowFactory.php b/src/TwbsHelper/Form/View/Helper/Factory/FormRowFactory.php index d5107bb17..8ea0fb095 100644 --- a/src/TwbsHelper/Form/View/Helper/Factory/FormRowFactory.php +++ b/src/TwbsHelper/Form/View/Helper/Factory/FormRowFactory.php @@ -7,7 +7,6 @@ namespace TwbsHelper\Form\View\Helper\Factory; - class FormRowFactory implements \Zend\ServiceManager\FactoryInterface { /** @@ -40,4 +39,4 @@ public function __invoke(\Interop\Container\ContainerInterface $oContainer, $sRe return new \TwbsHelper\Form\View\Helper\FormRow($oOptions); } -} \ No newline at end of file +}