Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom attributes to 'form', 'fieldset' and 'form-group' wrapper #25

Merged
merged 2 commits into from
Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

return [
'twbshelper' => [
'ignoredViewHelpers' => [
'ignoredViewHelpers' => [
'button',
'checkbox',
'file',
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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' => [
Expand All @@ -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',
],
Expand Down
42 changes: 42 additions & 0 deletions src/TwbsHelper/Form/View/Helper/Factory/FormCollectionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright (c) 2019.
*
* @author Syber
*/

namespace TwbsHelper\Form\View\Helper\Factory;

class FormCollectionFactory implements \Zend\ServiceManager\FactoryInterface
{
/**
* Compatibility with ZF2 (>= 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);
}
}
42 changes: 42 additions & 0 deletions src/TwbsHelper/Form/View/Helper/Factory/FormFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright (c) 2019.
*
* @author Syber
*/

namespace TwbsHelper\Form\View\Helper\Factory;

class FormFactory implements \Zend\ServiceManager\FactoryInterface
{
/**
* Compatibility with ZF2 (>= 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);
}
}
42 changes: 42 additions & 0 deletions src/TwbsHelper/Form/View/Helper/Factory/FormRowFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright (c) 2019.
*
* @author Syber
*/

namespace TwbsHelper\Form\View\Helper\Factory;

class FormRowFactory implements \Zend\ServiceManager\FactoryInterface
{
/**
* Compatibility with ZF2 (>= 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);
}
}
28 changes: 28 additions & 0 deletions src/TwbsHelper/Form/View/Helper/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down
29 changes: 29 additions & 0 deletions src/TwbsHelper/Form/View/Helper/FormCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,29 @@ class FormCollection extends \Zend\Form\View\Helper\FormCollection
protected static $fieldsetRegex = '/(<fieldset[^>]*>)([\s\S]*)(<\/fieldset[^>]*>)$/imU';
protected static $legendRegex = '/<legend[^>]*>([\s\S]*)<\/legend[^>]*>/imU';

// Hold configurable options
protected $options;

/**
* This is the default wrapper that the collection is wrapped into
*
* @var string
*/
protected $wrapper = '<fieldset%4$s>%2$s%1$s%3$s</fieldset>';


/**
* 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
*
Expand All @@ -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
Expand Down
33 changes: 33 additions & 0 deletions src/TwbsHelper/Form/View/Helper/FormRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down