Skip to content

Commit

Permalink
fix(Form): inherit form options to elements on form prepare
Browse files Browse the repository at this point in the history
  • Loading branch information
neilime committed Oct 27, 2022
1 parent 65ac483 commit 055666c
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build-php: ## Build PHP image for given version
@DOCKER_BUILDKIT=1 docker build -t "twbs-helper-php:$(filter-out $@,$(MAKECMDGOALS))" --build-arg "VERSION=$(filter-out $@,$(MAKECMDGOALS))" .

install:
@$(call run-php,$(filter-out $@,$(MAKECMDGOALS)) composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader)
@$(call run-php,$(filter-out $@,$(MAKECMDGOALS)) composer install --no-progress --prefer-dist --optimize-autoloader)

shell:
@$(call run-php,$(filter-out $@,$(MAKECMDGOALS)) bash)
Expand Down
112 changes: 107 additions & 5 deletions src/TwbsHelper/Form/View/Helper/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ class Form extends \Laminas\Form\View\Helper\Form
*/
protected $options;

/**
* @var null|\TwbsHelper\Form\View\Helper\FormRows
*/
protected $formRowsHelper;

/**
* @var null|\TwbsHelper\View\Helper\HtmlElement
*/
protected $htmlElementHelper;

/**
* @var null|\TwbsHelper\View\Helper\HtmlAttributes\HtmlClass
*/
protected $htmlClassHelper;

/**
* Constructor
*
Expand Down Expand Up @@ -103,15 +118,14 @@ public function render(\Laminas\Form\FormInterface $form): string
{
$this->prepareForm($form);

$elementsContent = $this->getView()->plugin('formRows')->__invoke($form);
$elementsContent = $this->getFormRowsHelper()->__invoke($form);
$elementsContent = empty($elementsContent)
? ''
: $this->getView()->plugin('htmlElement')->addProperIndentation($elementsContent, true);
: $this->getHtmlElementHelper()->addProperIndentation($elementsContent, true);

return $this->openTag($form) . $elementsContent . $this->closeTag();
}


protected function prepareForm(\Laminas\Form\FormInterface $form)
{
// Prepare form if needed
Expand All @@ -124,6 +138,12 @@ protected function prepareForm(\Laminas\Form\FormInterface $form)
$form->setAttribute('role', 'form');
}

$this->prepareFormClasses($form);
$this->inheritOptionsToElements($form);
}

protected function prepareFormClasses(\Laminas\Form\FormInterface $form)
{
$classes = [];

if ($form->getOption('custom_validation')) {
Expand All @@ -142,18 +162,100 @@ protected function prepareForm(\Laminas\Form\FormInterface $form)
if ($column) {
$classes = array_merge(
$classes,
$this->getView()->plugin('htmlClass')->plugin('column')->getClassesFromOption($column, 'row-cols'),
$this->getHtmlClassHelper()->plugin('column')->getClassesFromOption($column, 'row-cols'),
);
}

$gutter = $form->getOption('gutter');
if ($gutter) {
$classes = array_merge(
$classes,
$this->getView()->plugin('htmlClass')->plugin('gutter')->getClassesFromOption($gutter),
$this->getHtmlClassHelper()->plugin('gutter')->getClassesFromOption($gutter),
);
}
}
$this->setClassesToElement($form, $classes);
}

protected function inheritOptionsToElements(\Laminas\Form\FormInterface $form)
{
$formLayout = $form->getOption('layout');
$tooltipFeedback = $form->getOption('tooltip_feedback');

foreach ($form as $element) {
// Define layout option to form elements if not already defined
if ($formLayout && !$element->getOption('layout')) {
$element->setOption('layout', $formLayout);
}
// Define tooltip_feedback option to form elements if not already defined
if ($element->getOption('tooltip_feedback') === null) {
$element->setOption('tooltip_feedback', $tooltipFeedback);
}
}
}

/**
* Retrieve the formRow helper
*/
protected function getFormRowsHelper(): \TwbsHelper\Form\View\Helper\FormRows
{
if ($this->formRowsHelper) {
return $this->formRowsHelper;
}

if ($this->view !== null && method_exists($this->view, 'plugin')) {
$this->formRowsHelper = $this->view->plugin('formRows');
}

if (!$this->formRowsHelper instanceof \TwbsHelper\Form\View\Helper\FormRows) {
throw new \LogicException(sprintf(
'FormCollection helper expects an instanceof \TwbsHelper\Form\View\Helper\FormRows, "%s" defined',
is_object($this->formRowsHelper)
? get_class($this->formRowsHelper)
: gettype($this->formRowsHelper)
));
}

return $this->formRowsHelper;
}

/**
* Retrieve the htmlElement helper
*/
protected function getHtmlElementHelper()
{
if ($this->htmlElementHelper) {
return $this->htmlElementHelper;
}

if ($this->view !== null && method_exists($this->view, 'plugin')) {
$this->htmlElementHelper = $this->view->plugin('htmlElement');
}

if (!$this->htmlElementHelper instanceof \TwbsHelper\View\Helper\HtmlElement) {
$this->htmlElementHelper = new \TwbsHelper\View\Helper\HtmlElement();
}

return $this->htmlElementHelper;
}

/**
* Retrieve the htmlclass helper
*/
protected function getHtmlClassHelper()
{
if ($this->htmlClassHelper) {
return $this->htmlClassHelper;
}

if ($this->view !== null && method_exists($this->view, 'plugin')) {
$this->htmlClassHelper = $this->view->plugin('htmlClass');
}

if (!$this->htmlClassHelper instanceof \TwbsHelper\View\Helper\HtmlAttributes\HtmlClass) {
$this->htmlClassHelper = new \TwbsHelper\View\Helper\HtmlAttributes\HtmlClass();
}

return $this->htmlClassHelper;
}
}
12 changes: 0 additions & 12 deletions src/TwbsHelper/Form/View/Helper/FormRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,10 @@ protected function renderFormRows(\Laminas\Form\FormInterface $form): string
{
$rowClass = $form->getOption('row_class') ?? 'row';
$gutter = $form->getOption('gutter');
$formLayout = $form->getOption('layout');
$tooltipFeedback = $form->getOption('tooltip_feedback');

// Store element rows rendering
$rowsRendering = [];
foreach ($form as $element) {
// Define layout option to form elements if not already defined
if ($formLayout && !$element->getOption('layout')) {
$element->setOption('layout', $formLayout);
}

// Define tooltip_feedback option to form elements if not already defined
if ($element->getOption('tooltip_feedback') === null) {
$element->setOption('tooltip_feedback', $tooltipFeedback);
}

$rowsRendering = $this->renderElement($element, $rowsRendering, [
'row_class' => $rowClass,
'gutter' => $gutter,
Expand Down
21 changes: 20 additions & 1 deletion tests/TestSuite/TwbsHelper/Form/View/Helper/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,19 @@ public function testInvokeWithoutArgumentsReturnSelf()
);
}

public function testRenderEmptyForm()
{
$form = new \Laminas\Form\Form();

$this->assertEquals(
'<form action="" method="POST" role="form"></form>',
$this->formHelper->__invoke($form)
);
}

public function testInitializeAnHorizontalFormClass()
{
$horizontalFormClass = new class () extends \Laminas\Form\Form
$horizontalFormClass = new class() extends \Laminas\Form\Form
{
public function prepare()
{
Expand Down Expand Up @@ -71,4 +81,13 @@ public function prepare()
$this->formHelper->__invoke($newHorizontalForm)
);
}


public function testRenderSpecWithWrongFormElementThrowsAnException()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid spec specified, Laminas\Form\Element does not inherit from Laminas\Form\FormInterface.');

$this->formHelper->renderSpec(['type' => 'Element']);
}
}

0 comments on commit 055666c

Please sign in to comment.