diff --git a/src/TwbsHelper/Form/View/Helper/FormRow.php b/src/TwbsHelper/Form/View/Helper/FormRow.php index 5fcbf1b58..570914c31 100644 --- a/src/TwbsHelper/Form/View/Helper/FormRow.php +++ b/src/TwbsHelper/Form/View/Helper/FormRow.php @@ -118,10 +118,6 @@ public function renderFormRow(\Laminas\Form\ElementInterface $oElement, $sElemen $aRowClasses[] = 'has-error'; } - if ($oElement->getOption('feedback')) { - $aRowClasses[] = 'has-feedback'; - } - // Column $sColum = $oElement->getOption('column'); if ($sColum) { @@ -211,18 +207,18 @@ protected function renderElement(\Laminas\Form\ElementInterface $oElement, strin case null: case \TwbsHelper\Form\View\Helper\Form::LAYOUT_INLINE: $aRenderingOrder = [ - 'renderFeedback' => [], 'renderLabel' => [$sLabelPosition], 'renderHelpBlock' => [], 'renderErrors' => [], + 'renderValidFeedback' => [], 'renderDedicatedContainer' => [], ]; break; case \TwbsHelper\Form\View\Helper\Form::LAYOUT_HORIZONTAL: $aRenderingOrder = [ - 'renderFeedback' => [], 'renderHelpBlock' => [], 'renderErrors' => [], + 'renderValidFeedback' => [], 'renderDedicatedContainer' => [], ]; break; @@ -381,35 +377,29 @@ protected function renderHelpBlock(\Laminas\Form\ElementInterface $oElement, str * @param \Laminas\Form\ElementInterface $oElement * @return string */ - protected function renderErrors(\Laminas\Form\ElementInterface $oElement, string $sElementContent): string + protected function renderValidFeedback(\Laminas\Form\ElementInterface $oElement, string $sElementContent): string { - if ($this->renderErrors) { - $sElementErrorsContent = $this->getElementErrorsHelper()->render($oElement); - if ($sElementErrorsContent) { - $sElementContent .= PHP_EOL . $sElementErrorsContent; - } + $sValidFeedback = $oElement->getOption('valid_feedback'); + if ($sValidFeedback) { + $sValidFeedbackContent = $this->htmlElement('div',['class' => 'valid-feedback'], $sValidFeedback); + $sElementContent .= PHP_EOL . $sValidFeedbackContent; } return $sElementContent; } /** - * Render element's feedback + * Render element's errors * * @param \Laminas\Form\ElementInterface $oElement - * @param string $sElementContent * @return string */ - protected function renderFeedback(\Laminas\Form\ElementInterface $oElement, string $sElementContent): string + protected function renderErrors(\Laminas\Form\ElementInterface $oElement, string $sElementContent): string { - $sFeedback = $oElement->getOption('feedback'); - if ($sFeedback) { - if (!is_string($sFeedback)) { - throw new \InvalidArgumentException(sprintf( - 'Argument "$sFeedbackElement" expects a string, "%s" given', - is_object($sFeedback) ? get_class($sFeedback) : gettype($sFeedback) - )); + if ($this->renderErrors) { + $sElementErrorsContent = $this->getElementErrorsHelper()->render($oElement); + if ($sElementErrorsContent) { + $sElementContent .= PHP_EOL . $sElementErrorsContent; } - $sElementContent .= ''; } return $sElementContent; } diff --git a/tests/TestSuite/Documentation/Components/Forms.php b/tests/TestSuite/Documentation/Components/Forms.php index bf7896c36..dbec448ba 100644 --- a/tests/TestSuite/Documentation/Components/Forms.php +++ b/tests/TestSuite/Documentation/Components/Forms.php @@ -12,6 +12,7 @@ include __DIR__ . DIRECTORY_SEPARATOR . 'Forms/Layout.php', include __DIR__ . DIRECTORY_SEPARATOR . 'Forms/HelpText.php', include __DIR__ . DIRECTORY_SEPARATOR . 'Forms/DisabledForms.php', + include __DIR__ . DIRECTORY_SEPARATOR . 'Forms/Validation.php', include __DIR__ . DIRECTORY_SEPARATOR . 'Forms/CustomForms.php', ], ]; diff --git a/tests/TestSuite/Documentation/Components/Forms/Validation.php b/tests/TestSuite/Documentation/Components/Forms/Validation.php new file mode 100644 index 000000000..f5fd00908 --- /dev/null +++ b/tests/TestSuite/Documentation/Components/Forms/Validation.php @@ -0,0 +1,191 @@ + 'Validation', + 'url' => '%bootstrap-url%/components/forms/#validation', + 'tests' => [ + [ + 'title' => 'Server side', + 'url' => '%bootstrap-url%/components/forms/#server-side', + 'rendering' => function (\Laminas\View\Renderer\PhpRenderer $oView) { + $oFactory = new \Laminas\Form\Factory(); + + $oForm = $oFactory->create([ + 'type' => 'form', + 'options' => ['row_class' => 'form-row'], + 'elements' => [ + [ + 'spec' => [ + 'name' => 'firstName', + 'options' => [ + 'column' => 'md-6', + 'row_class' => 'mb-3', + 'label' => 'First name', + 'valid_feedback' => 'Looks good!', + 'row_name' => 'firstRow', + ], + 'attributes' => [ + 'type' => 'text', + 'value' => 'Mark', + 'id' => 'validationServer01', + 'required' => true, + ], + ], + ], + [ + 'spec' => [ + 'name' => 'lastName', + 'options' => [ + 'column' => 'md-6', + 'row_class' => 'mb-3', + 'label' => 'Last name', + 'valid_feedback' => 'Looks good!', + 'row_name' => 'firstRow', + ], + 'attributes' => [ + 'type' => 'text', + 'value' => 'Otto', + 'id' => 'validationServer02', + 'required' => true, + ], + ], + ], + [ + 'spec' => [ + 'name' => 'city', + 'options' => [ + 'column' => 'md-6', + 'row_class' => 'mb-3', + 'label' => 'City', + 'row_name' => 'secondRow', + ], + 'attributes' => [ + 'type' => 'text', + 'id' => 'validationServer03', + 'required' => true, + ], + ], + ], + [ + 'spec' => [ + 'name' => 'state', + 'type' => 'select', + 'options' => [ + 'custom' => true, + 'empty_option' => ['label' => 'Choose...', 'selected' => true, 'disabled' => true], + 'value_options' => ['...'], + 'column' => 'md-3', + 'row_class' => 'mb-3', + 'label' => 'State', + 'row_name' => 'secondRow', + ], + 'attributes' => [ + 'id' => 'validationServer04', + 'required' => true, + ], + ], + ], + [ + 'spec' => [ + 'name' => 'zip', + 'options' => [ + 'column' => 'md-3', + 'row_class' => 'mb-3', + 'label' => 'Zip', + 'row_name' => 'secondRow', + ], + 'attributes' => [ + 'type' => 'text', + 'id' => 'validationServer05', + 'required' => true, + ], + ], + ], + [ + 'spec' => [ + 'name' => 'termsAndConditions', + 'type' => 'checkbox', + 'options' => [ + 'label' => 'Agree to terms and conditions', + 'use_hidden_element' => false, + 'row_name' => 'thirdRow', + ], + 'attributes' => [ + 'id' => 'invalidCheck3', + 'required' => true, + ], + ], + ], + [ + 'spec' => [ + 'type' => 'submit', + 'options' => [ + 'label' => 'Submit', 'variant' => 'primary', + 'row_name' => 'lastRow', + 'form_group' => false, + ], + ], + ], + ], + ]); + + // Set error messages + $oForm->get('city')->setMessages(['Please provide a valid city.']); + $oForm->get('state')->setMessages(['Please select a valid state.']); + $oForm->get('zip')->setMessages(['Please provide a valid zip.']); + $oForm->get('termsAndConditions')->setMessages(['You must agree before submitting.']); + + + // Render form + echo $oView->form($oForm); + }, + 'expected' => '
' . PHP_EOL . + '
' . PHP_EOL . + '
' . PHP_EOL . + ' ' . PHP_EOL . + ' ' . PHP_EOL . + '
' . PHP_EOL . + ' Looks good!' . PHP_EOL . + '
' . PHP_EOL . + '
' . PHP_EOL . + '
' . PHP_EOL . + ' ' . PHP_EOL . + ' ' . PHP_EOL . + '
' . PHP_EOL . + ' Looks good!' . PHP_EOL . + '
' . PHP_EOL . + '
' . PHP_EOL . + '
' . PHP_EOL . + '
' . PHP_EOL . + '
' . PHP_EOL . + ' ' . PHP_EOL . + ' ' . PHP_EOL . + '
Please provide a valid city.
' . PHP_EOL . + '
' . PHP_EOL . + '
' . PHP_EOL . + ' ' . PHP_EOL . + ' ' . PHP_EOL . + '
Please select a valid state.
' . PHP_EOL . + '
' . PHP_EOL . + '
' . PHP_EOL . + ' ' . PHP_EOL . + ' ' . PHP_EOL . + '
Please provide a valid zip.
' . PHP_EOL . + '
' . PHP_EOL . + '
' . PHP_EOL . + '
' . PHP_EOL . + '
' . PHP_EOL . + ' ' . PHP_EOL . + ' ' . PHP_EOL . + '
You must agree before submitting.
' . PHP_EOL . + '
' . PHP_EOL . + '
' . PHP_EOL . + ' ' . PHP_EOL . + '
', + ], + ], +];