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 c16f905
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 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
32 changes: 27 additions & 5 deletions src/TwbsHelper/Form/View/Helper/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,14 @@ public function render(\Laminas\Form\FormInterface $form): string
{
$this->prepareForm($form);

$elementsContent = $this->getView()->plugin('formRows')->__invoke($form);
$elementsContent = $this->view->plugin('formRows')->__invoke($form);
$elementsContent = empty($elementsContent)
? ''
: $this->getView()->plugin('htmlElement')->addProperIndentation($elementsContent, true);
: $this->view->plugin('htmlElement')->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 +123,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 +147,35 @@ protected function prepareForm(\Laminas\Form\FormInterface $form)
if ($column) {
$classes = array_merge(
$classes,
$this->getView()->plugin('htmlClass')->plugin('column')->getClassesFromOption($column, 'row-cols'),
$this->view->plugin('htmlClass')->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->view->plugin('htmlClass')->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);
}
}
}
}
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

0 comments on commit c16f905

Please sign in to comment.