From 74f135e5bc2a13e94964b5a7ce2ea2093d473406 Mon Sep 17 00:00:00 2001 From: vEnCa-X Date: Fri, 29 Dec 2023 16:44:21 +0100 Subject: [PATCH 01/14] refactoring test to separate folder --- tests/tests/{ => TbV4}/BootstrapBaseRendererV4.phpt | 2 +- tests/tests/{ => TbV4}/BootstrapHorizontalRendererV4.phpt | 6 +++--- tests/tests/{ => TbV4}/BootstrapVerticalRendererV4.phpt | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) rename tests/tests/{ => TbV4}/BootstrapBaseRendererV4.phpt (96%) rename tests/tests/{ => TbV4}/BootstrapHorizontalRendererV4.phpt (96%) rename tests/tests/{ => TbV4}/BootstrapVerticalRendererV4.phpt (96%) diff --git a/tests/tests/BootstrapBaseRendererV4.phpt b/tests/tests/TbV4/BootstrapBaseRendererV4.phpt similarity index 96% rename from tests/tests/BootstrapBaseRendererV4.phpt rename to tests/tests/TbV4/BootstrapBaseRendererV4.phpt index 5aa1a58..cc9b292 100644 --- a/tests/tests/BootstrapBaseRendererV4.phpt +++ b/tests/tests/TbV4/BootstrapBaseRendererV4.phpt @@ -10,7 +10,7 @@ use Tester; use Tester\Assert; use VencaX; -require __DIR__ . '/../bootstrap.php'; +require __DIR__ . '/../../bootstrap.php'; class BootstrapVerticalRendererV4 extends Tester\TestCase { diff --git a/tests/tests/BootstrapHorizontalRendererV4.phpt b/tests/tests/TbV4/BootstrapHorizontalRendererV4.phpt similarity index 96% rename from tests/tests/BootstrapHorizontalRendererV4.phpt rename to tests/tests/TbV4/BootstrapHorizontalRendererV4.phpt index 96009e0..48f587e 100644 --- a/tests/tests/BootstrapHorizontalRendererV4.phpt +++ b/tests/tests/TbV4/BootstrapHorizontalRendererV4.phpt @@ -9,7 +9,7 @@ use Tester; use Tester\Assert; use VencaX; -require __DIR__ . '/../bootstrap.php'; +require __DIR__ . '/../../bootstrap.php'; class BootstrapHorizontalRendererV4 extends Tester\TestCase { @@ -32,7 +32,7 @@ class BootstrapHorizontalRendererV4 extends Tester\TestCase $form = $this->addInputs($form); - Assert::matchFile(__DIR__ . '/../expected/bootstrap-v4/form-horizontal.html', (string) $form); + Assert::matchFile(__DIR__ . '/../../expected/bootstrap-v4/form-horizontal.html', (string) $form); } @@ -49,7 +49,7 @@ class BootstrapHorizontalRendererV4 extends Tester\TestCase $form = $this->addInputs($form); - Assert::matchFile(__DIR__ . '/../expected/bootstrap-v4/form-horizontal-sm-6.html', (string) $form); + Assert::matchFile(__DIR__ . '/../../expected/bootstrap-v4/form-horizontal-sm-6.html', (string) $form); } diff --git a/tests/tests/BootstrapVerticalRendererV4.phpt b/tests/tests/TbV4/BootstrapVerticalRendererV4.phpt similarity index 96% rename from tests/tests/BootstrapVerticalRendererV4.phpt rename to tests/tests/TbV4/BootstrapVerticalRendererV4.phpt index 915f93f..e04202d 100644 --- a/tests/tests/BootstrapVerticalRendererV4.phpt +++ b/tests/tests/TbV4/BootstrapVerticalRendererV4.phpt @@ -9,7 +9,7 @@ use Tester; use Tester\Assert; use VencaX; -require __DIR__ . '/../bootstrap.php'; +require __DIR__ . '/../../bootstrap.php'; class BootstrapVerticalRendererV4 extends Tester\TestCase { @@ -27,7 +27,7 @@ class BootstrapVerticalRendererV4 extends Tester\TestCase $form = $this->addInputs($form); - Assert::matchFile(__DIR__ . '/../expected/bootstrap-v4/form-vertical.html', (string) $form); + Assert::matchFile(__DIR__ . '/../../expected/bootstrap-v4/form-vertical.html', (string) $form); } @@ -41,7 +41,7 @@ class BootstrapVerticalRendererV4 extends Tester\TestCase $form = $this->addInputs($form); - Assert::matchFile(__DIR__ . '/../expected/bootstrap-v4/form-vertical.html', (string) $form); + Assert::matchFile(__DIR__ . '/../../expected/bootstrap-v4/form-vertical.html', (string) $form); } From 1cb539e9f536367a3997d22ff3d702229d525055 Mon Sep 17 00:00:00 2001 From: vEnCa-X Date: Fri, 29 Dec 2023 18:50:10 +0100 Subject: [PATCH 02/14] wip --- composer.json | 3 +- src/BootstrapRendererV5.php | 395 ++++++++++++++++++ tests/expected/bootstrap-v5/form-base.html | 23 + .../bootstrap-v5/form-horizontal.html | 0 .../BootstrapBaseRendererV4.phpt | 2 +- .../BootstrapHorizontalRendererV4.phpt | 2 +- .../BootstrapVerticalRendererV4.phpt | 2 +- .../BoostrapV5/BootstrapBaseRendererV5.phpt | 50 +++ .../BootstrapHorizontalRendererV5.phpt | 198 +++++++++ 9 files changed, 670 insertions(+), 5 deletions(-) create mode 100644 src/BootstrapRendererV5.php create mode 100644 tests/expected/bootstrap-v5/form-base.html create mode 100644 tests/expected/bootstrap-v5/form-horizontal.html rename tests/tests/{TbV4 => BoostrapV4}/BootstrapBaseRendererV4.phpt (97%) rename tests/tests/{TbV4 => BoostrapV4}/BootstrapHorizontalRendererV4.phpt (99%) rename tests/tests/{TbV4 => BoostrapV4}/BootstrapVerticalRendererV4.phpt (99%) create mode 100644 tests/tests/BoostrapV5/BootstrapBaseRendererV5.phpt create mode 100644 tests/tests/BoostrapV5/BootstrapHorizontalRendererV5.phpt diff --git a/composer.json b/composer.json index 3570969..23f8c0e 100644 --- a/composer.json +++ b/composer.json @@ -13,8 +13,7 @@ "nette/forms": "^3.0" }, "require-dev": { - "nette/tester": "^2.0", - "phpstan/phpstan": "^0.12.45@dev" + "nette/tester": "^2.0" }, "autoload": { "classmap": [ diff --git a/src/BootstrapRendererV5.php b/src/BootstrapRendererV5.php new file mode 100644 index 0000000..f9e908a --- /dev/null +++ b/src/BootstrapRendererV5.php @@ -0,0 +1,395 @@ +wrappers['error']['container'] = 'div'; + $renderer->wrappers['error']['item'] = 'div class="alert alert-danger"'; + + $this->wrappers['controls']['container'] = null; + if ($this->isFormVerticalOrientation()) { + $this->wrappers['pair']['container'] = 'div class="row mb-3"'; //vertical + } else { + $this->wrappers['pair']['container'] = 'div class="form-group row"'; //horizontal + } + $this->wrappers['pair']['.error'] = 'has-danger'; + if ($this->isFormVerticalOrientation()) { + $this->wrappers['control']['container'] = null; //vertical + } else { + $this->wrappers['control']['container'] = 'div class="' . $this->formControlContainerWidth . '"'; //horizontal + } + if ($this->isFormVerticalOrientation()) { + $this->wrappers['label']['container'] = null; //vertical + } else { + $this->wrappers['label']['container'] = 'div class="' . $this->formControlLabelWidth . ' col-form-label"'; //horizontal + } + $this->wrappers['control']['checkbox'] = 'div class="form-check"'; + $this->wrappers['control']['description'] = 'small'; + $this->wrappers['control']['errorcontainer'] = 'span class=form-control-feedback'; + + if ($this->isFormInline()) { + $this->form->getElementPrototype()->addClass('form-inline'); + } + + return parent::renderBegin(); + } + + + /** + * Set vertical form oriantation (is default) + */ + public function setFormVerticalOrientation() + { + $this->formOrientationVertical = true; + } + + + /** + * Set horizontal form oriantation + */ + public function setFormHorizontalOrientation() + { + $this->formOrientationVertical = false; + } + + + /** + * Is form in vertical orientation + * @return bool + */ + private function isFormVerticalOrientation(): bool + { + return $this->formOrientationVertical; + } + + + public function setFormInline() + { + $this->formInline = true; + } + + + /** + * Is form in inline + * @return bool + */ + private function isFormInline(): bool + { + return $this->formInline; + } + + + public function setFormControlContainerWidth(string $formControlContainerWidth) + { + $this->formControlContainerWidth = $formControlContainerWidth; + } + + + public function setFormControlLabelWidth(string $formControlLabelWidth) + { + $this->formControlLabelWidth = $formControlLabelWidth; + } + + + private function generateRadioControls(Nette\Forms\IControl $control, ?Html $labelPart): Html + { + $fieldset = Html::el('fieldset')->addClass('form-group'); + if ($labelPart != '') { + $fieldset->addHtml(Html::el('legend')->addHtml($labelPart)); + } + + foreach ($control->items as $key => $labelTitle) { + $input = $control->getControlPart($key)->addClass('form-check-input'); //input + $label = $control->getLabelPart($key)->addClass('form-check-label'); //label + + $formCheck = Html::el('div')->addClass('form-check'); + if ($control->getOption('orientation', null) == self::FORM_CHECK_INLINE) { + $formCheck->class(self::FORM_CHECK_INLINE, true); + } + + $formCheck->addHtml($input); + $formCheck->addHtml($label); + + $fieldset->addHtml($formCheck); + } + + return $fieldset; + } + + + /** + * Renders single visual row. + */ + public function renderPair(Nette\Forms\IControl $control): string + { + if ( + $control->getOption('type') === 'radio' + && $control->getOption('orientation', null) == self::FORM_CHECK_INLINE + ) { + $radios = Html::el(null); + + $pair = $this->getWrapper('pair container'); + + //title for radio + if ($this->isFormVerticalOrientation()) { + //vertical form, one line with title, next line with radios + $pair->addHtml($this->generateRadioControls($control, $control->getLabelPart())); + } else { + //horizontal form, one linew with title and with radios + $labelContainer = $this->getWrapper('label container')->addHtml($control->getLabelPart()); + $controlContainer = $this->getWrapper('control container'); + $controlContainer->addHtml($this->generateRadioControls($control, null)); + + $pair->addHtml($labelContainer); + $pair->addHtml($controlContainer); + } + + $radios->addHtml($pair); //add pari with title and radios on one line + return $radios->render(0); + + } elseif ($control->getOption('type') === 'checkbox') { + if ($this->isFormVerticalOrientation()) { + //default vertical orientation + $pair = Html::el('div')->addClass('form-check'); + if ($control->getOption('orientation', null) == self::FORM_CHECK_INLINE) { + $pair->class(self::FORM_CHECK_INLINE, true); + } + } else { + //horizontal formular (2 colms) + if ($control->getOption('orientation', null) == self::FORM_CHECK_INLINE) { + $pair = $this->getWrapper('pair container'); + } else { + $pair = $this->getWrapper('pair container'); + //@TODO how to set many checkboxes on same line? problem... + } + } + + if ($this->isFormInline()) { + $pair->class('mb-2 mr-sm-2 mb-sm-0', true); + } + + } else { + $pair = $this->getWrapper('pair container'); + } + + $pair->addHtml($this->renderLabel($control)); + $pair->addHtml($this->renderControl($control)); + $pair->class($this->getValue($control->isRequired() ? 'pair .required' : 'pair .optional'), true); + $pair->class($control->hasErrors() ? $this->getValue('pair .error') : null, true); + $pair->class($control->getOption('class'), true); + if (++$this->counter % 2) { + $pair->class($this->getValue('pair .odd'), true); + } + $pair->id = $control->getOption('id'); + return $pair->render(0); + } + + + /** + * Renders single visual row of multiple controls (SubmitButton). + * @param Nette\Forms\IControl[] + */ + public function renderPairMulti(array $controls): string + { + $s = []; + foreach ($controls as $control) { + $description = $control->getOption('description'); + + $control->setOption('rendered', true); + $el = $control->getControl(); + if ($el instanceof Html && $el->getName() === 'input') { + $el->class($this->getValue("control .$el->type"), true); + } + $s[] = $el . $description; + } + if ($this->isFormVerticalOrientation()) { + //is vertical form + $pair = Html::el(''); + $divForControl = Html::el(''); + } else { + //is horizontal form (2colums) + $pair = $this->getWrapper('pair container'); + $divForControl = Html::el('div')->addClass('col'); + } + + $pair->addHtml($this->renderLabel($control)); + $pair->addHtml($divForControl->setHtml(implode(' ', $s))); + return $pair->render(0); + } + + + /** + * Renders 'label' part of visual row of controls. + */ + public function renderLabel(Nette\Forms\IControl $control): Html + { + if ($control->getOption('type') === 'button') { + //none label for label + return Html::el(''); + } else { + $suffix = $this->getValue('label suffix') . ($control->isRequired() ? $this->getValue('label requiredsuffix') : ''); + $label = $control->getLabel(); + + $label->class('form-label', true); + + if ($label instanceof Html) { + $label->addHtml($suffix); + if ($control->isRequired()) { + $label->class($this->getValue('control .required'), true); + } + } + + if ($control->getOption('type') === 'radio' && $this->isFormVerticalOrientation()) { + //label for radio is in fieldset, not shwo here + $label = ''; + } + + return $this->getWrapper('label container')->setHtml((string) $label); + } + } + + + /** + * Renders 'control' part of visual row of controls. + */ + public function renderControl(Nette\Forms\IControl $control): Html + { + $body = $this->getWrapper('control container'); + if ($this->counter % 2) { + $body->class($this->getValue('control .odd'), true); + } + + $description = $control->getOption('description'); + if ($description != null) { // intentionally == + if ($control instanceof Nette\Forms\Controls\BaseControl) { + $description = $control->translate($description); + } + $description = ' ' . $this->getWrapper('control description')->setText($description); + + } else { + $description = ''; + } + + if ($control->isRequired()) { + $description = $this->getValue('control requiredsuffix') . $description; + } + + $control->setOption('rendered', true); + + if (in_array($control->getOption('type'), ['checkbox', 'radio'], true)) { + if ($control instanceof Nette\Forms\Controls\Checkbox) { + $control->getLabelPrototype()->addClass('form-check-label'); + } else { + $control->getItemLabelPrototype()->addClass('form-check'); + $control->getItemLabelPrototype()->addClass('form-check-label'); + } + $control->getControlPrototype()->addClass('form-check-input'); + + if ($control->getOption('type') == 'checkbox') { + //checkbox + if ($this->isFormVerticalOrientation()) { + $el = Html::el(''); + } else { + $el = $this->getWrapper('control checkbox'); + } + + if ($control->getOption('orientation', null) == self::FORM_CHECK_INLINE) { + $el->class(self::FORM_CHECK_INLINE, true); + } + + $input = $control->getControlPart()->addClass('form-check-input'); //input + $label = $control->getLabelPart()->addClass('form-check-label'); //label + + $el->addHtml($input); + $el->addHtml($label); + } else { + //radio + $input = $control->getControlPart(); + $items = $control->getItems(); + $ids = []; + $values = []; + + if ($control->generateId) { + foreach ($items as $value => $label) { + $ids[$value] = $input->id . '-' . $value; + $values[$value] = $value; + } + } + $elControl = $control->getContainerPrototype()->setHtml( + Nette\Forms\Helpers::createInputList( + $control->translate($items), + array_merge($input->attrs, [ + 'id:' => $ids, + 'value:' => $values, + 'checked?' => $control->getValue(), + 'disabled:' => $control->isDisabled(), + //'data-nette-rules:' => [key($items) => $input->attrs['data-nette-rules']], + ]), + ['for:' => $ids] + $control->getItemLabelPrototype()->attrs, + //$control->getSeparatorPrototype() + Html::el('div') + ) + ); + + $el = Html::el('fieldset')->addClass('form-group'); + if ($this->isFormVerticalOrientation()) { + $el->addHtml(Html::el('legend')->addHtml($control->getLabelPart())); + } + $el->addHtml($elControl); + } + + } else { + $el = $control->getControl(); + } + + if ( + $control->getOption('type') === 'text' + || $control->getOption('type') === 'textarea' + || $control->getOption('type') === 'select' + ) { + $el->class('form-control', true); + + if ($this->isFormInline()) { + $el->class('mb-2 mr-sm-2 mb-sm-0', true); + } + + } elseif ($control->getOption('type') === 'file') { + $el->class('form-control-file', true); + } else { + $el->class($this->getValue("control .$el->type"), true); + } + + return $body->setHtml($el . $description . $this->renderErrors($control)); + } +} diff --git a/tests/expected/bootstrap-v5/form-base.html b/tests/expected/bootstrap-v5/form-base.html new file mode 100644 index 0000000..73114af --- /dev/null +++ b/tests/expected/bootstrap-v5/form-base.html @@ -0,0 +1,23 @@ +
+ + +
+ + + +
+ +
+ + + +
+ + + + + + + + +
diff --git a/tests/expected/bootstrap-v5/form-horizontal.html b/tests/expected/bootstrap-v5/form-horizontal.html new file mode 100644 index 0000000..e69de29 diff --git a/tests/tests/TbV4/BootstrapBaseRendererV4.phpt b/tests/tests/BoostrapV4/BootstrapBaseRendererV4.phpt similarity index 97% rename from tests/tests/TbV4/BootstrapBaseRendererV4.phpt rename to tests/tests/BoostrapV4/BootstrapBaseRendererV4.phpt index cc9b292..b2912ba 100644 --- a/tests/tests/TbV4/BootstrapBaseRendererV4.phpt +++ b/tests/tests/BoostrapV4/BootstrapBaseRendererV4.phpt @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Test; +namespace Test\BoostrapV4; use Nette\Forms\Form; use Nette\Utils\Html; diff --git a/tests/tests/TbV4/BootstrapHorizontalRendererV4.phpt b/tests/tests/BoostrapV4/BootstrapHorizontalRendererV4.phpt similarity index 99% rename from tests/tests/TbV4/BootstrapHorizontalRendererV4.phpt rename to tests/tests/BoostrapV4/BootstrapHorizontalRendererV4.phpt index 48f587e..ca90891 100644 --- a/tests/tests/TbV4/BootstrapHorizontalRendererV4.phpt +++ b/tests/tests/BoostrapV4/BootstrapHorizontalRendererV4.phpt @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Test; +namespace Test\BoostrapV4; use Nette\Forms\Form; use Tester; diff --git a/tests/tests/TbV4/BootstrapVerticalRendererV4.phpt b/tests/tests/BoostrapV4/BootstrapVerticalRendererV4.phpt similarity index 99% rename from tests/tests/TbV4/BootstrapVerticalRendererV4.phpt rename to tests/tests/BoostrapV4/BootstrapVerticalRendererV4.phpt index e04202d..541a4cb 100644 --- a/tests/tests/TbV4/BootstrapVerticalRendererV4.phpt +++ b/tests/tests/BoostrapV4/BootstrapVerticalRendererV4.phpt @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Test; +namespace Test\BoostrapV4; use Nette\Forms\Form; use Tester; diff --git a/tests/tests/BoostrapV5/BootstrapBaseRendererV5.phpt b/tests/tests/BoostrapV5/BootstrapBaseRendererV5.phpt new file mode 100644 index 0000000..20920de --- /dev/null +++ b/tests/tests/BoostrapV5/BootstrapBaseRendererV5.phpt @@ -0,0 +1,50 @@ +setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV5); + return $form; + } + + + public function testBaseForm() + { + $form = $this->createBaseFormWithRenderer(); + + $form->addEmail('exampleFormControlInput1', 'Email address') + ->setHtmlAttribute('placeholder', 'name@example.com'); + + $form->addTextArea('exampleFormControlTextarea1', 'Example textarea') + ->setRequired(); + + $form->addSubmit('submit', 'Submit')->setHtmlAttribute('class', 'btn btn-primary'); + + $html = (string) $form; + + Assert::matchFile(__DIR__ . '/../../expected/bootstrap-v5/form-base.html', $html); + + $dom = Tester\DomQuery::fromHtml($html); + + //labels + Assert::same('Email address', (string) $dom->find('label')[0]); + Assert::same('Example textarea', (string) $dom->find('label')[1]); + } +} + +$test = new BootstrapVerticalRendererV5; +$test->run(); diff --git a/tests/tests/BoostrapV5/BootstrapHorizontalRendererV5.phpt b/tests/tests/BoostrapV5/BootstrapHorizontalRendererV5.phpt new file mode 100644 index 0000000..0ff79ed --- /dev/null +++ b/tests/tests/BoostrapV5/BootstrapHorizontalRendererV5.phpt @@ -0,0 +1,198 @@ +createBaseFormWithRenderer(); + + //$html = (string) $this->addInputs($form); + + //Assert::same('', $html); + + //Assert::matchFile(__DIR__ . '/../../expected/bootstrap-v5/form-horizontal.html', (string) $form); + Assert::same(0, 0); + } + + /* + public function testHorizontalFormSm6() + { + $form = $this->createBaseFormWithRenderer(); + + //horizontal form + $renderer = $form->getRenderer(); + $renderer->setFormHorizontalOrientation(); + + $renderer->setFormControlLabelWidth('col-sm-6'); + $renderer->setFormControlContainerWidth('col-sm-6'); + + $form = $this->addInputs($form); + + Assert::matchFile(__DIR__ . '/../../expected/bootstrap-v5/form-horizontal-sm-6.html', (string) $form); + } + */ + + private function createBaseFormWithRenderer() + { + $form = new Form; + $form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV5); + + //horizontal form + $renderer = $form->getRenderer(); + $renderer->setFormHorizontalOrientation(); + + return $form; + } + + private function addInputs(Form $form): Form + { + $form->addEmail('exampleInputEmail1', 'Email address:') + ->setHtmlAttribute('placeholder', 'Enter email') + ->setOption('description', 'We\'ll never share your email with anyone else.'); + + $form->addPassword('exampleInputPassword1', 'Password') + ->setHtmlAttribute('placeholder', 'Password'); +/* + $form->addCheckbox('checkbox', 'Check me out'); + $form->addCheckbox('checkbox2', 'Check me out2'); +*/ + $form->addRadioList('country', 'Country', [ + 'cz' => 'Česká republika', + 'sk' => 'Slovensko', + 'eu' => 'EU', + ]); + + $form->addEmail('exampleInputEmail2', 'Email address') + ->setHtmlAttribute('placeholder', 'name@example.com'); + + $form->addSelect('exampleSelect', 'Example select', [ + '1', + '2', + '3', + '4', + '5', ]); + + $form->addMultiSelect('exampleMultipleSelect', 'Example multiple select', [ + '1', + '2', + '3', + '4', + '5', ]); + + $form->addTextArea('textarea', 'Example textarea'); + + $form->addUpload('upload', 'Example upload'); + + $form->addMultiUpload('multiUpload', 'Example multiUpload'); + + //sizes + $form->addEmail('formControlLg', '.form-control-lg') + ->setHtmlAttribute('class', 'form-control-lg') + ->setHtmlAttribute('placeholder', '.form-control-lg'); + + $form->addEmail('formControl', '.form-control') + ->setHtmlAttribute('placeholder', 'Default input'); + + $form->addEmail('formControlSm', '.form-control-sm') + ->setHtmlAttribute('class', 'form-control-sm') + ->setHtmlAttribute('placeholder', '.form-control-sm'); + + //sizes select + $form->addSelect('largeSelect', 'Large select', ['Large select']) + ->setHtmlAttribute('class', 'form-control-lg'); + + $form->addSelect('defaultSelect', ' Default select', ['Default select']); + + $form->addSelect('smallSelect', 'Small select', ['Small select']) + ->setHtmlAttribute('class', 'form-control-sm'); + + //disables + $form->addText('disabled', 'Disabled:') + ->setHtmlAttribute('placeholder', 'Disabled input here…') + ->setDisabled(true); + + + $form->addSubmit('submit', 'Submit')->setHtmlAttribute('class', 'btn btn-primary'); + + return $form; + } + +/* + public function testVerticalCheckInlineForm() + { + $form = $this->createBaseFormWithRenderer(); + + $form->addCheckbox('mondayCheckbox', 'Monday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('tuesdayCheckbox', 'Tuesday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('wednesdayCheckbox', 'Wednesday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('thurstdayCheckbox', 'Thurstday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('fridayCheckbox', 'Friday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('saturdayCheckbox', 'Saturday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('sundayCheckbox', 'Sunday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + + $form->addRadioList('weekRadio', 'Week radio', [ + 'monday' => 'Monday', + 'tuesday' => 'Tuesday', + 'wednesday' => 'Wednesday', + 'thurstday' => 'Thurstday', + 'friday' => 'Friday', + 'saturday' => 'Saturday', + 'sunday' => 'Sunday', + ])->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + + $html = (string) $form; + + //Assert::same('', (string) $html); + + $dom = Tester\DomQuery::fromHtml($html); + + $this->checkInlineCheckbox($dom, 0, 'Monday'); + $this->checkInlineCheckbox($dom, 1, 'Tuesday'); + $this->checkInlineCheckbox($dom, 2, 'Wednesday'); + $this->checkInlineCheckbox($dom, 3, 'Thurstday'); + $this->checkInlineCheckbox($dom, 4, 'Friday'); + $this->checkInlineCheckbox($dom, 5, 'Saturday'); + $this->checkInlineCheckbox($dom, 6, 'Sunday'); + + Assert::same('form-group row', (string) $dom->find('div.form-group')[7]->attributes()['class']); + Assert::same('Week radio', (string) $dom->find('div.form-group.row div.col-sm-3.col-form-label label')[0]); + $this->checkInlineRadio($dom, 7, 'Monday'); + $this->checkInlineRadio($dom, 8, 'Tuesday'); + $this->checkInlineRadio($dom, 9, 'Wednesday'); + $this->checkInlineRadio($dom, 10, 'Thurstday'); + $this->checkInlineRadio($dom, 11, 'Friday'); + $this->checkInlineRadio($dom, 12, 'Saturday'); + } + + + private function checkInlineCheckbox($dom, $position, $label) + { + Assert::contains('form-group row', (string) $dom->find('div.form-group.row')[$position]->attributes()['class']); + Assert::same($label, (string) $dom->find('div label')[$position]); + Assert::contains('form-check-label', (string) $dom->find('div label')[$position]->attributes()['class']); + Assert::contains('form-check-input', (string) $dom->find('div input')[$position]->attributes()['class']); + } + + + private function checkInlineRadio($dom, $position, $label) + { + Assert::contains($label, (string) $dom->find('div.form-group.row div.col-sm-9 label')[$position]); + Assert::contains('form-check-label', (string) $dom->find('div.form-group.row div.col-sm-9 label')[$position]->attributes()['class']); + Assert::contains('form-check-input', (string) $dom->find('div.form-group.row div.col-sm-9 input')[$position]->attributes()['class']); + } + */ +} + +$test = new BootstrapHorizontalRendererV5; +$test->run(); From 69a86ea823027045893d1c9ff7a3d6e9406aad5d Mon Sep 17 00:00:00 2001 From: vEnCa-X Date: Fri, 29 Dec 2023 19:17:38 +0100 Subject: [PATCH 03/14] cs --- src/BootstrapRendererV5.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/BootstrapRendererV5.php b/src/BootstrapRendererV5.php index f9e908a..311a24c 100644 --- a/src/BootstrapRendererV5.php +++ b/src/BootstrapRendererV5.php @@ -39,7 +39,7 @@ public function renderBegin(): string $this->wrappers['controls']['container'] = null; if ($this->isFormVerticalOrientation()) { - $this->wrappers['pair']['container'] = 'div class="row mb-3"'; //vertical + $this->wrappers['pair']['container'] = 'div class="mb-3"'; //vertical } else { $this->wrappers['pair']['container'] = 'div class="form-group row"'; //horizontal } @@ -122,7 +122,7 @@ public function setFormControlLabelWidth(string $formControlLabelWidth) } - private function generateRadioControls(Nette\Forms\IControl $control, ?Html $labelPart): Html + private function generateRadioControls(Nette\Forms\Control $control, ?Html $labelPart): Html { $fieldset = Html::el('fieldset')->addClass('form-group'); if ($labelPart != '') { @@ -151,7 +151,7 @@ private function generateRadioControls(Nette\Forms\IControl $control, ?Html $lab /** * Renders single visual row. */ - public function renderPair(Nette\Forms\IControl $control): string + public function renderPair(Nette\Forms\Control $control): string { if ( $control->getOption('type') === 'radio' @@ -218,7 +218,7 @@ public function renderPair(Nette\Forms\IControl $control): string /** * Renders single visual row of multiple controls (SubmitButton). - * @param Nette\Forms\IControl[] + * @param Nette\Forms\Control[] */ public function renderPairMulti(array $controls): string { @@ -252,7 +252,7 @@ public function renderPairMulti(array $controls): string /** * Renders 'label' part of visual row of controls. */ - public function renderLabel(Nette\Forms\IControl $control): Html + public function renderLabel(Nette\Forms\Control $control): Html { if ($control->getOption('type') === 'button') { //none label for label @@ -261,9 +261,8 @@ public function renderLabel(Nette\Forms\IControl $control): Html $suffix = $this->getValue('label suffix') . ($control->isRequired() ? $this->getValue('label requiredsuffix') : ''); $label = $control->getLabel(); - $label->class('form-label', true); - if ($label instanceof Html) { + $label->class('form-label', true); $label->addHtml($suffix); if ($control->isRequired()) { $label->class($this->getValue('control .required'), true); @@ -283,7 +282,7 @@ public function renderLabel(Nette\Forms\IControl $control): Html /** * Renders 'control' part of visual row of controls. */ - public function renderControl(Nette\Forms\IControl $control): Html + public function renderControl(Nette\Forms\Control $control): Html { $body = $this->getWrapper('control container'); if ($this->counter % 2) { From 37a8e20d61c86fda92adf78adb44ed58ad3eeaff Mon Sep 17 00:00:00 2001 From: vEnCa-X Date: Fri, 29 Dec 2023 19:43:32 +0100 Subject: [PATCH 04/14] tests --- src/BootstrapRendererV5.php | 6 +- tests/expected/bootstrap-v5/form-base.html | 14 +- .../bootstrap-v5/form-horizontal-sm-6.html | 119 +++++++ .../bootstrap-v5/form-horizontal.html | 119 +++++++ .../expected/bootstrap-v5/form-vertical.html | 119 +++++++ .../BoostrapV5/BootstrapBaseRendererV5.phpt | 4 +- .../BootstrapHorizontalRendererV5.phpt | 336 +++++++++--------- .../BootstrapVerticalRendererV5.phpt | 219 ++++++++++++ 8 files changed, 760 insertions(+), 176 deletions(-) create mode 100644 tests/expected/bootstrap-v5/form-horizontal-sm-6.html create mode 100644 tests/expected/bootstrap-v5/form-vertical.html create mode 100644 tests/tests/BoostrapV5/BootstrapVerticalRendererV5.phpt diff --git a/src/BootstrapRendererV5.php b/src/BootstrapRendererV5.php index 311a24c..9dea9d2 100644 --- a/src/BootstrapRendererV5.php +++ b/src/BootstrapRendererV5.php @@ -27,6 +27,8 @@ class BootstrapRendererV5 extends Nette\Forms\Rendering\DefaultFormRenderer private $formControlContainerWidth = 'col-sm-9'; + private $formVerticalDivMb = 'mb-3'; + /** * Renders form begin. @@ -43,6 +45,7 @@ public function renderBegin(): string } else { $this->wrappers['pair']['container'] = 'div class="form-group row"'; //horizontal } + $this->wrappers['pair']['checkbox'] = 'div class="' . $this->formVerticalDivMb . ' form-check"'; //horizontal $this->wrappers['pair']['.error'] = 'has-danger'; if ($this->isFormVerticalOrientation()) { $this->wrappers['control']['container'] = null; //vertical @@ -181,7 +184,8 @@ public function renderPair(Nette\Forms\Control $control): string } elseif ($control->getOption('type') === 'checkbox') { if ($this->isFormVerticalOrientation()) { //default vertical orientation - $pair = Html::el('div')->addClass('form-check'); + $pair = $this->getWrapper('pair checkbox'); + if ($control->getOption('orientation', null) == self::FORM_CHECK_INLINE) { $pair->class(self::FORM_CHECK_INLINE, true); } diff --git a/tests/expected/bootstrap-v5/form-base.html b/tests/expected/bootstrap-v5/form-base.html index 73114af..dc47480 100644 --- a/tests/expected/bootstrap-v5/form-base.html +++ b/tests/expected/bootstrap-v5/form-base.html @@ -1,23 +1,29 @@
-
+
-
+
+
+ + + +
+ + - - + \ No newline at end of file diff --git a/tests/expected/bootstrap-v5/form-horizontal-sm-6.html b/tests/expected/bootstrap-v5/form-horizontal-sm-6.html new file mode 100644 index 0000000..412d414 --- /dev/null +++ b/tests/expected/bootstrap-v5/form-horizontal-sm-6.html @@ -0,0 +1,119 @@ +
+ + +
+
+ +
We'll never share your email with anyone else.
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+ + +
+
+ + +
\ No newline at end of file diff --git a/tests/expected/bootstrap-v5/form-horizontal.html b/tests/expected/bootstrap-v5/form-horizontal.html index e69de29..9ac03f1 100644 --- a/tests/expected/bootstrap-v5/form-horizontal.html +++ b/tests/expected/bootstrap-v5/form-horizontal.html @@ -0,0 +1,119 @@ +
+ + +
+
+ +
We'll never share your email with anyone else.
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+ + +
+
+ + +
\ No newline at end of file diff --git a/tests/expected/bootstrap-v5/form-vertical.html b/tests/expected/bootstrap-v5/form-vertical.html new file mode 100644 index 0000000..44af5b6 --- /dev/null +++ b/tests/expected/bootstrap-v5/form-vertical.html @@ -0,0 +1,119 @@ +
+ + +
+ + + We'll never share your email with anyone else. +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + +
+
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ + + + + + + + +
\ No newline at end of file diff --git a/tests/tests/BoostrapV5/BootstrapBaseRendererV5.phpt b/tests/tests/BoostrapV5/BootstrapBaseRendererV5.phpt index 20920de..db07760 100644 --- a/tests/tests/BoostrapV5/BootstrapBaseRendererV5.phpt +++ b/tests/tests/BoostrapV5/BootstrapBaseRendererV5.phpt @@ -32,7 +32,9 @@ class BootstrapVerticalRendererV5 extends Tester\TestCase $form->addTextArea('exampleFormControlTextarea1', 'Example textarea') ->setRequired(); - $form->addSubmit('submit', 'Submit')->setHtmlAttribute('class', 'btn btn-primary'); + $form->addCheckbox('checkbox', 'Check me out'); + + $form->addSubmit('submit', 'Submit')->setHtmlAttribute('class', 'btn btn-primary'); $html = (string) $form; diff --git a/tests/tests/BoostrapV5/BootstrapHorizontalRendererV5.phpt b/tests/tests/BoostrapV5/BootstrapHorizontalRendererV5.phpt index 0ff79ed..50218be 100644 --- a/tests/tests/BoostrapV5/BootstrapHorizontalRendererV5.phpt +++ b/tests/tests/BoostrapV5/BootstrapHorizontalRendererV5.phpt @@ -13,185 +13,181 @@ require __DIR__ . '/../../bootstrap.php'; class BootstrapHorizontalRendererV5 extends Tester\TestCase { - public function testHorizontalForm() - { - $form = $this->createBaseFormWithRenderer(); + public function testHorizontalForm() + { + $form = $this->createBaseFormWithRenderer(); + $html = (string)$this->addInputs($form); - //$html = (string) $this->addInputs($form); + Assert::matchFile(__DIR__ . '/../../expected/bootstrap-v5/form-horizontal.html', $html); + } - //Assert::same('', $html); - - //Assert::matchFile(__DIR__ . '/../../expected/bootstrap-v5/form-horizontal.html', (string) $form); - Assert::same(0, 0); - } - /* - public function testHorizontalFormSm6() - { - $form = $this->createBaseFormWithRenderer(); + public function testHorizontalFormSm6() + { + $form = $this->createBaseFormWithRenderer(); - //horizontal form - $renderer = $form->getRenderer(); - $renderer->setFormHorizontalOrientation(); + //horizontal form + $renderer = $form->getRenderer(); + $renderer->setFormHorizontalOrientation(); - $renderer->setFormControlLabelWidth('col-sm-6'); - $renderer->setFormControlContainerWidth('col-sm-6'); + $renderer->setFormControlLabelWidth('col-sm-6'); + $renderer->setFormControlContainerWidth('col-sm-6'); - $form = $this->addInputs($form); + $html = (string)$this->addInputs($form); - Assert::matchFile(__DIR__ . '/../../expected/bootstrap-v5/form-horizontal-sm-6.html', (string) $form); - } - */ + Assert::matchFile(__DIR__ . '/../../expected/bootstrap-v5/form-horizontal-sm-6.html', $html); + } - private function createBaseFormWithRenderer() - { - $form = new Form; - $form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV5); - - //horizontal form - $renderer = $form->getRenderer(); - $renderer->setFormHorizontalOrientation(); - - return $form; - } - private function addInputs(Form $form): Form - { - $form->addEmail('exampleInputEmail1', 'Email address:') - ->setHtmlAttribute('placeholder', 'Enter email') - ->setOption('description', 'We\'ll never share your email with anyone else.'); - - $form->addPassword('exampleInputPassword1', 'Password') - ->setHtmlAttribute('placeholder', 'Password'); -/* - $form->addCheckbox('checkbox', 'Check me out'); - $form->addCheckbox('checkbox2', 'Check me out2'); -*/ - $form->addRadioList('country', 'Country', [ - 'cz' => 'Česká republika', - 'sk' => 'Slovensko', - 'eu' => 'EU', - ]); - - $form->addEmail('exampleInputEmail2', 'Email address') - ->setHtmlAttribute('placeholder', 'name@example.com'); - - $form->addSelect('exampleSelect', 'Example select', [ - '1', - '2', - '3', - '4', - '5', ]); - - $form->addMultiSelect('exampleMultipleSelect', 'Example multiple select', [ - '1', - '2', - '3', - '4', - '5', ]); - - $form->addTextArea('textarea', 'Example textarea'); - - $form->addUpload('upload', 'Example upload'); - - $form->addMultiUpload('multiUpload', 'Example multiUpload'); - - //sizes - $form->addEmail('formControlLg', '.form-control-lg') - ->setHtmlAttribute('class', 'form-control-lg') - ->setHtmlAttribute('placeholder', '.form-control-lg'); - - $form->addEmail('formControl', '.form-control') - ->setHtmlAttribute('placeholder', 'Default input'); - - $form->addEmail('formControlSm', '.form-control-sm') - ->setHtmlAttribute('class', 'form-control-sm') - ->setHtmlAttribute('placeholder', '.form-control-sm'); - - //sizes select - $form->addSelect('largeSelect', 'Large select', ['Large select']) - ->setHtmlAttribute('class', 'form-control-lg'); - - $form->addSelect('defaultSelect', ' Default select', ['Default select']); - - $form->addSelect('smallSelect', 'Small select', ['Small select']) - ->setHtmlAttribute('class', 'form-control-sm'); - - //disables - $form->addText('disabled', 'Disabled:') - ->setHtmlAttribute('placeholder', 'Disabled input here…') - ->setDisabled(true); - - - $form->addSubmit('submit', 'Submit')->setHtmlAttribute('class', 'btn btn-primary'); - - return $form; - } - -/* - public function testVerticalCheckInlineForm() - { - $form = $this->createBaseFormWithRenderer(); - - $form->addCheckbox('mondayCheckbox', 'Monday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); - $form->addCheckbox('tuesdayCheckbox', 'Tuesday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); - $form->addCheckbox('wednesdayCheckbox', 'Wednesday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); - $form->addCheckbox('thurstdayCheckbox', 'Thurstday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); - $form->addCheckbox('fridayCheckbox', 'Friday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); - $form->addCheckbox('saturdayCheckbox', 'Saturday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); - $form->addCheckbox('sundayCheckbox', 'Sunday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); - - $form->addRadioList('weekRadio', 'Week radio', [ - 'monday' => 'Monday', - 'tuesday' => 'Tuesday', - 'wednesday' => 'Wednesday', - 'thurstday' => 'Thurstday', - 'friday' => 'Friday', - 'saturday' => 'Saturday', - 'sunday' => 'Sunday', - ])->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); - - $html = (string) $form; - - //Assert::same('', (string) $html); - - $dom = Tester\DomQuery::fromHtml($html); - - $this->checkInlineCheckbox($dom, 0, 'Monday'); - $this->checkInlineCheckbox($dom, 1, 'Tuesday'); - $this->checkInlineCheckbox($dom, 2, 'Wednesday'); - $this->checkInlineCheckbox($dom, 3, 'Thurstday'); - $this->checkInlineCheckbox($dom, 4, 'Friday'); - $this->checkInlineCheckbox($dom, 5, 'Saturday'); - $this->checkInlineCheckbox($dom, 6, 'Sunday'); - - Assert::same('form-group row', (string) $dom->find('div.form-group')[7]->attributes()['class']); - Assert::same('Week radio', (string) $dom->find('div.form-group.row div.col-sm-3.col-form-label label')[0]); - $this->checkInlineRadio($dom, 7, 'Monday'); - $this->checkInlineRadio($dom, 8, 'Tuesday'); - $this->checkInlineRadio($dom, 9, 'Wednesday'); - $this->checkInlineRadio($dom, 10, 'Thurstday'); - $this->checkInlineRadio($dom, 11, 'Friday'); - $this->checkInlineRadio($dom, 12, 'Saturday'); - } - - - private function checkInlineCheckbox($dom, $position, $label) - { - Assert::contains('form-group row', (string) $dom->find('div.form-group.row')[$position]->attributes()['class']); - Assert::same($label, (string) $dom->find('div label')[$position]); - Assert::contains('form-check-label', (string) $dom->find('div label')[$position]->attributes()['class']); - Assert::contains('form-check-input', (string) $dom->find('div input')[$position]->attributes()['class']); - } - - - private function checkInlineRadio($dom, $position, $label) - { - Assert::contains($label, (string) $dom->find('div.form-group.row div.col-sm-9 label')[$position]); - Assert::contains('form-check-label', (string) $dom->find('div.form-group.row div.col-sm-9 label')[$position]->attributes()['class']); - Assert::contains('form-check-input', (string) $dom->find('div.form-group.row div.col-sm-9 input')[$position]->attributes()['class']); - } - */ + private function createBaseFormWithRenderer() + { + $form = new Form; + $form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV5); + + //horizontal form + $renderer = $form->getRenderer(); + $renderer->setFormHorizontalOrientation(); + + return $form; + } + + private function addInputs(Form $form): Form + { + $form->addEmail('exampleInputEmail1', 'Email address:') + ->setHtmlAttribute('placeholder', 'Enter email') + ->setOption('description', 'We\'ll never share your email with anyone else.'); + + $form->addPassword('exampleInputPassword1', 'Password') + ->setHtmlAttribute('placeholder', 'Password'); + + $form->addCheckbox('checkbox', 'Check me out'); + $form->addCheckbox('checkbox2', 'Check me out2'); + + $form->addRadioList('country', 'Country', [ + 'cz' => 'Česká republika', + 'sk' => 'Slovensko', + 'eu' => 'EU', + ]); + + $form->addEmail('exampleInputEmail2', 'Email address') + ->setHtmlAttribute('placeholder', 'name@example.com'); + + $form->addSelect('exampleSelect', 'Example select', [ + '1', + '2', + '3', + '4', + '5',]); + + $form->addMultiSelect('exampleMultipleSelect', 'Example multiple select', [ + '1', + '2', + '3', + '4', + '5',]); + + $form->addTextArea('textarea', 'Example textarea'); + + $form->addUpload('upload', 'Example upload'); + + $form->addMultiUpload('multiUpload', 'Example multiUpload'); + + //sizes + $form->addEmail('formControlLg', '.form-control-lg') + ->setHtmlAttribute('class', 'form-control-lg') + ->setHtmlAttribute('placeholder', '.form-control-lg'); + + $form->addEmail('formControl', '.form-control') + ->setHtmlAttribute('placeholder', 'Default input'); + + $form->addEmail('formControlSm', '.form-control-sm') + ->setHtmlAttribute('class', 'form-control-sm') + ->setHtmlAttribute('placeholder', '.form-control-sm'); + + //sizes select + $form->addSelect('largeSelect', 'Large select', ['Large select']) + ->setHtmlAttribute('class', 'form-control-lg'); + + $form->addSelect('defaultSelect', ' Default select', ['Default select']); + + $form->addSelect('smallSelect', 'Small select', ['Small select']) + ->setHtmlAttribute('class', 'form-control-sm'); + + //disables + $form->addText('disabled', 'Disabled:') + ->setHtmlAttribute('placeholder', 'Disabled input here…') + ->setDisabled(true); + + + $form->addSubmit('submit', 'Submit')->setHtmlAttribute('class', 'btn btn-primary'); + + return $form; + } + + + public function testVerticalCheckInlineForm() + { + $form = $this->createBaseFormWithRenderer(); + + $form->addCheckbox('mondayCheckbox', 'Monday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('tuesdayCheckbox', 'Tuesday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('wednesdayCheckbox', 'Wednesday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('thurstdayCheckbox', 'Thurstday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('fridayCheckbox', 'Friday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('saturdayCheckbox', 'Saturday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('sundayCheckbox', 'Sunday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + + $form->addRadioList('weekRadio', 'Week radio', [ + 'monday' => 'Monday', + 'tuesday' => 'Tuesday', + 'wednesday' => 'Wednesday', + 'thurstday' => 'Thurstday', + 'friday' => 'Friday', + 'saturday' => 'Saturday', + 'sunday' => 'Sunday', + ])->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + + $html = (string)$form; + + //Assert::same('', (string) $html); + + $dom = Tester\DomQuery::fromHtml($html); + + $this->checkInlineCheckbox($dom, 0, 'Monday'); + $this->checkInlineCheckbox($dom, 1, 'Tuesday'); + $this->checkInlineCheckbox($dom, 2, 'Wednesday'); + $this->checkInlineCheckbox($dom, 3, 'Thurstday'); + $this->checkInlineCheckbox($dom, 4, 'Friday'); + $this->checkInlineCheckbox($dom, 5, 'Saturday'); + $this->checkInlineCheckbox($dom, 6, 'Sunday'); + + Assert::same('form-group row', (string)$dom->find('div.form-group')[7]->attributes()['class']); + Assert::same('Week radio', (string)$dom->find('div.form-group.row div.col-sm-3.col-form-label label')[0]); + $this->checkInlineRadio($dom, 7, 'Monday'); + $this->checkInlineRadio($dom, 8, 'Tuesday'); + $this->checkInlineRadio($dom, 9, 'Wednesday'); + $this->checkInlineRadio($dom, 10, 'Thurstday'); + $this->checkInlineRadio($dom, 11, 'Friday'); + $this->checkInlineRadio($dom, 12, 'Saturday'); + } + + + private function checkInlineCheckbox($dom, $position, $label) + { + Assert::contains('form-group row', (string)$dom->find('div.form-group.row')[$position]->attributes()['class']); + Assert::same($label, (string)$dom->find('div label')[$position]); + Assert::contains('form-check-label', (string)$dom->find('div label')[$position]->attributes()['class']); + Assert::contains('form-check-input', (string)$dom->find('div input')[$position]->attributes()['class']); + } + + + private function checkInlineRadio($dom, $position, $label) + { + Assert::contains($label, (string)$dom->find('div.form-group.row div.col-sm-9 label')[$position]); + Assert::contains('form-check-label', (string)$dom->find('div.form-group.row div.col-sm-9 label')[$position]->attributes()['class']); + Assert::contains('form-check-input', (string)$dom->find('div.form-group.row div.col-sm-9 input')[$position]->attributes()['class']); + } + } $test = new BootstrapHorizontalRendererV5; diff --git a/tests/tests/BoostrapV5/BootstrapVerticalRendererV5.phpt b/tests/tests/BoostrapV5/BootstrapVerticalRendererV5.phpt new file mode 100644 index 0000000..efdf44c --- /dev/null +++ b/tests/tests/BoostrapV5/BootstrapVerticalRendererV5.phpt @@ -0,0 +1,219 @@ +setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV5); + return $form; + } + + + public function testVerticalForm() + { + $form = $this->createBaseFormWithRenderer(); + + $form = $this->addInputs($form); + + Assert::matchFile(__DIR__ . '/../../expected/bootstrap-v5/form-vertical.html', (string) $form); + } + + + public function testVerticalTwoForm() + { + $form = $this->createBaseFormWithRenderer(); + + //vertical form + $renderer = $form->getRenderer(); + $renderer->setFormVerticalOrientation(); + + $form = $this->addInputs($form); + + Assert::matchFile(__DIR__ . '/../../expected/bootstrap-v5/form-vertical.html', (string) $form); + } + + + private function addInputs(Form $form): Form + { + $form->addEmail('exampleInputEmail1', 'Email address:') + ->setHtmlAttribute('placeholder', 'Enter email') + ->setOption('description', 'We\'ll never share your email with anyone else.'); + $form->addPassword('exampleInputPassword1', 'Password') + ->setHtmlAttribute('placeholder', 'Password'); + $form->addCheckbox('checkbox', 'Check me out'); + $form->addCheckbox('checkbox2', 'Check me out2'); + + $form->addRadioList('country', 'Country', [ + 'cz' => 'Česká republika', + 'sk' => 'Slovensko', + 'eu' => 'EU', + ]); + + $form->addEmail('exampleInputEmail2', 'Email address') + ->setHtmlAttribute('placeholder', 'name@example.com'); + + $form->addSelect('exampleSelect', 'Example select', [ + '1', + '2', + '3', + '4', + '5', ]); + + $form->addMultiSelect('exampleMultipleSelect', 'Example multiple select', [ + '1', + '2', + '3', + '4', + '5', ]); + + $form->addTextArea('textarea', 'Example textarea'); + + $form->addUpload('upload', 'Example upload'); + + $form->addMultiUpload('multiUpload', 'Example multiUpload'); + + + //sizes + $form->addEmail('formControlLg', '.form-control-lg') + ->setHtmlAttribute('class', 'form-control-lg') + ->setHtmlAttribute('placeholder', '.form-control-lg'); + + $form->addEmail('formControl', '.form-control') + ->setHtmlAttribute('placeholder', 'Default input'); + + $form->addEmail('formControlSm', '.form-control-sm') + ->setHtmlAttribute('class', 'form-control-sm') + ->setHtmlAttribute('placeholder', '.form-control-sm'); + + + //sizes select + $form->addSelect('largeSelect', 'Large select', ['Large select']) + ->setHtmlAttribute('class', 'form-control-lg'); + + $form->addSelect('defaultSelect', ' Default select', ['Default select']); + + $form->addSelect('smallSelect', 'Small select', ['Small select']) + ->setHtmlAttribute('class', 'form-control-sm'); + + + //disables + $form->addText('disabled', 'Disabled:') + ->setHtmlAttribute('placeholder', 'Disabled input here…') + ->setDisabled(true); + + + $form->addSubmit('submit', 'Submit')->setHtmlAttribute('class', 'btn btn-primary'); + + return $form; + } + + + public function testVerticalCheckInlineForm() + { + $form = $this->createBaseFormWithRenderer(); + + $form->addCheckbox('mondayCheckbox', 'Monday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('tuesdayCheckbox', 'Tuesday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('wednesdayCheckbox', 'Wednesday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('thurstdayCheckbox', 'Thurstday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('fridayCheckbox', 'Friday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('saturdayCheckbox', 'Saturday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('sundayCheckbox', 'Sunday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + + $form->addRadioList('weekRadio', 'Week radio', [ + 'monday' => 'Monday', + 'tuesday' => 'Tuesday', + 'wednesday' => 'Wednesday', + 'thurstday' => 'Thurstday', + 'friday' => 'Friday', + 'saturday' => 'Saturday', + 'sunday' => 'Sunday', + ])->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + + $html = (string) $form; + + //Assert::same('', (string) $html); + + $dom = Tester\DomQuery::fromHtml($html); + + $this->checkInlineCheckbox($dom, 0, 'Monday'); + $this->checkInlineCheckbox($dom, 1, 'Tuesday'); + $this->checkInlineCheckbox($dom, 2, 'Wednesday'); + $this->checkInlineCheckbox($dom, 3, 'Thurstday'); + $this->checkInlineCheckbox($dom, 4, 'Friday'); + $this->checkInlineCheckbox($dom, 5, 'Saturday'); + $this->checkInlineCheckbox($dom, 6, 'Sunday'); + + Assert::same('Week radio', (string) $dom->find('fieldset legend label')[0]); + Assert::same('form-check form-check-inline', (string) $dom->find('div.form-check')[8]->attributes()['class']); + $this->checkInlineRadio($dom, 7, 'Monday'); + $this->checkInlineRadio($dom, 8, 'Tuesday'); + $this->checkInlineRadio($dom, 9, 'Wednesday'); + $this->checkInlineRadio($dom, 10, 'Thurstday'); + $this->checkInlineRadio($dom, 11, 'Friday'); + $this->checkInlineRadio($dom, 12, 'Saturday'); + } + + + public function testVerticalInlineForm() + { + $form = $this->createBaseFormWithRenderer(); + + //inline form + $renderer = $form->getRenderer(); + $renderer->setFormInline(); + + $form->addEmail('loginemail', 'E-mail address:') + ->setHtmlAttribute('placeholder', 'Enter e-mail'); + $form->addPassword('password', 'Password') + ->setHtmlAttribute('placeholder', 'Password'); + $form->addCheckbox('checkbox', 'Check me out'); + + $form->addSubmit('submit', 'Login')->setHtmlAttribute('class', 'btn btn-primary'); + + $html = (string) $form; + + //Assert::same('', (string) $html); + + $dom = Tester\DomQuery::fromHtml($html); + + Assert::same('form-inline', (string) $dom->find('form.form-inline')[0]->attributes()['class']); + + Assert::same('form-control mb-2 mr-sm-2 mb-sm-0', (string) $dom->find('input')[0]->attributes()['class']); + Assert::same('form-control mb-2 mr-sm-2 mb-sm-0', (string) $dom->find('input')[1]->attributes()['class']); + Assert::same('form-check-input', (string) $dom->find('input')[2]->attributes()['class']); + Assert::same('mb-3 form-check mb-2 mr-sm-2 mb-sm-0', (string) $dom->find('div')[2]->attributes()['class']); + } + + + private function checkInlineCheckbox($dom, $position, $label) + { + Assert::contains('form-check form-check-inline', (string) $dom->find('div.form-check.form-check-inline')[$position]->attributes()['class']); + Assert::same($label, (string) $dom->find('div label')[$position]); + Assert::contains('form-check-label', (string) $dom->find('div label')[$position]->attributes()['class']); + Assert::contains('form-check-input', (string) $dom->find('div input')[$position]->attributes()['class']); + } + + + private function checkInlineRadio($dom, $position, $label) + { + Assert::contains($label, (string) $dom->find('div.form-check label')[$position]); + Assert::contains('form-check-label', (string) $dom->find('div.form-check label')[$position]->attributes()['class']); + Assert::contains('form-check-input', (string) $dom->find('div input')[$position]->attributes()['class']); + } +} + +$test = new BootstrapVerticalRendererV5; +$test->run(); From fc6f1dcf2bbbaf995721183005bfe286c3df24bd Mon Sep 17 00:00:00 2001 From: vEnCa-X Date: Fri, 29 Dec 2023 19:44:34 +0100 Subject: [PATCH 05/14] coding standard --- src/BootstrapRendererV4.php | 2 +- src/BootstrapRendererV5.php | 6 +- tests/bootstrap.php | 3 +- .../BoostrapV4/BootstrapBaseRendererV4.phpt | 6 +- .../BootstrapHorizontalRendererV4.phpt | 6 +- .../BootstrapVerticalRendererV4.phpt | 6 +- .../BoostrapV5/BootstrapBaseRendererV5.phpt | 7 +- .../BootstrapHorizontalRendererV5.phpt | 274 +++++++++--------- .../BootstrapVerticalRendererV5.phpt | 6 +- 9 files changed, 158 insertions(+), 158 deletions(-) diff --git a/src/BootstrapRendererV4.php b/src/BootstrapRendererV4.php index 4d4956b..21a5d7c 100644 --- a/src/BootstrapRendererV4.php +++ b/src/BootstrapRendererV4.php @@ -33,7 +33,7 @@ class BootstrapRendererV4 extends Nette\Forms\Rendering\DefaultFormRenderer */ public function renderBegin(): string { - $renderer = new \stdClass; + $renderer = new \stdClass(); $renderer->wrappers['error']['container'] = 'div'; $renderer->wrappers['error']['item'] = 'div class="alert alert-danger"'; diff --git a/src/BootstrapRendererV5.php b/src/BootstrapRendererV5.php index 9dea9d2..2d52691 100644 --- a/src/BootstrapRendererV5.php +++ b/src/BootstrapRendererV5.php @@ -27,7 +27,7 @@ class BootstrapRendererV5 extends Nette\Forms\Rendering\DefaultFormRenderer private $formControlContainerWidth = 'col-sm-9'; - private $formVerticalDivMb = 'mb-3'; + private $formVerticalDivMb = 'mb-3'; /** @@ -35,7 +35,7 @@ class BootstrapRendererV5 extends Nette\Forms\Rendering\DefaultFormRenderer */ public function renderBegin(): string { - $renderer = new \stdClass; + $renderer = new \stdClass(); $renderer->wrappers['error']['container'] = 'div'; $renderer->wrappers['error']['item'] = 'div class="alert alert-danger"'; @@ -45,7 +45,7 @@ public function renderBegin(): string } else { $this->wrappers['pair']['container'] = 'div class="form-group row"'; //horizontal } - $this->wrappers['pair']['checkbox'] = 'div class="' . $this->formVerticalDivMb . ' form-check"'; //horizontal + $this->wrappers['pair']['checkbox'] = 'div class="' . $this->formVerticalDivMb . ' form-check"'; //horizontal $this->wrappers['pair']['.error'] = 'has-danger'; if ($this->isFormVerticalOrientation()) { $this->wrappers['control']['container'] = null; //vertical diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 6f89545..3a5ed9f 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,6 +1,7 @@ setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV4); + $form = new Form(); + $form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV4()); return $form; } @@ -50,5 +50,5 @@ class BootstrapVerticalRendererV4 extends Tester\TestCase } } -$test = new BootstrapVerticalRendererV4; +$test = new BootstrapVerticalRendererV4(); $test->run(); diff --git a/tests/tests/BoostrapV4/BootstrapHorizontalRendererV4.phpt b/tests/tests/BoostrapV4/BootstrapHorizontalRendererV4.phpt index ca90891..0b8c70d 100644 --- a/tests/tests/BoostrapV4/BootstrapHorizontalRendererV4.phpt +++ b/tests/tests/BoostrapV4/BootstrapHorizontalRendererV4.phpt @@ -15,8 +15,8 @@ class BootstrapHorizontalRendererV4 extends Tester\TestCase { private function createBaseFormWithRenderer() { - $form = new Form; - $form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV4); + $form = new Form(); + $form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV4()); //horizontal form $renderer = $form->getRenderer(); @@ -191,5 +191,5 @@ class BootstrapHorizontalRendererV4 extends Tester\TestCase } } -$test = new BootstrapHorizontalRendererV4; +$test = new BootstrapHorizontalRendererV4(); $test->run(); diff --git a/tests/tests/BoostrapV4/BootstrapVerticalRendererV4.phpt b/tests/tests/BoostrapV4/BootstrapVerticalRendererV4.phpt index 541a4cb..2cbdce0 100644 --- a/tests/tests/BoostrapV4/BootstrapVerticalRendererV4.phpt +++ b/tests/tests/BoostrapV4/BootstrapVerticalRendererV4.phpt @@ -15,8 +15,8 @@ class BootstrapVerticalRendererV4 extends Tester\TestCase { private function createBaseFormWithRenderer() { - $form = new Form; - $form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV4); + $form = new Form(); + $form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV4()); return $form; } @@ -215,5 +215,5 @@ class BootstrapVerticalRendererV4 extends Tester\TestCase } } -$test = new BootstrapVerticalRendererV4; +$test = new BootstrapVerticalRendererV4(); $test->run(); diff --git a/tests/tests/BoostrapV5/BootstrapBaseRendererV5.phpt b/tests/tests/BoostrapV5/BootstrapBaseRendererV5.phpt index db07760..c19a01a 100644 --- a/tests/tests/BoostrapV5/BootstrapBaseRendererV5.phpt +++ b/tests/tests/BoostrapV5/BootstrapBaseRendererV5.phpt @@ -5,7 +5,6 @@ declare(strict_types=1); namespace Test\BoostrapV5; use Nette\Forms\Form; -use Nette\Utils\Html; use Tester; use Tester\Assert; use VencaX; @@ -16,8 +15,8 @@ class BootstrapVerticalRendererV5 extends Tester\TestCase { private function createBaseFormWithRenderer() { - $form = new Form; - $form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV5); + $form = new Form(); + $form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV5()); return $form; } @@ -48,5 +47,5 @@ class BootstrapVerticalRendererV5 extends Tester\TestCase } } -$test = new BootstrapVerticalRendererV5; +$test = new BootstrapVerticalRendererV5(); $test->run(); diff --git a/tests/tests/BoostrapV5/BootstrapHorizontalRendererV5.phpt b/tests/tests/BoostrapV5/BootstrapHorizontalRendererV5.phpt index 50218be..c6dc3d6 100644 --- a/tests/tests/BoostrapV5/BootstrapHorizontalRendererV5.phpt +++ b/tests/tests/BoostrapV5/BootstrapHorizontalRendererV5.phpt @@ -13,182 +13,182 @@ require __DIR__ . '/../../bootstrap.php'; class BootstrapHorizontalRendererV5 extends Tester\TestCase { - public function testHorizontalForm() - { - $form = $this->createBaseFormWithRenderer(); - $html = (string)$this->addInputs($form); + public function testHorizontalForm() + { + $form = $this->createBaseFormWithRenderer(); + $html = (string) $this->addInputs($form); - Assert::matchFile(__DIR__ . '/../../expected/bootstrap-v5/form-horizontal.html', $html); - } + Assert::matchFile(__DIR__ . '/../../expected/bootstrap-v5/form-horizontal.html', $html); + } - public function testHorizontalFormSm6() - { - $form = $this->createBaseFormWithRenderer(); + public function testHorizontalFormSm6() + { + $form = $this->createBaseFormWithRenderer(); - //horizontal form - $renderer = $form->getRenderer(); - $renderer->setFormHorizontalOrientation(); + //horizontal form + $renderer = $form->getRenderer(); + $renderer->setFormHorizontalOrientation(); - $renderer->setFormControlLabelWidth('col-sm-6'); - $renderer->setFormControlContainerWidth('col-sm-6'); + $renderer->setFormControlLabelWidth('col-sm-6'); + $renderer->setFormControlContainerWidth('col-sm-6'); - $html = (string)$this->addInputs($form); + $html = (string) $this->addInputs($form); - Assert::matchFile(__DIR__ . '/../../expected/bootstrap-v5/form-horizontal-sm-6.html', $html); - } + Assert::matchFile(__DIR__ . '/../../expected/bootstrap-v5/form-horizontal-sm-6.html', $html); + } - private function createBaseFormWithRenderer() - { - $form = new Form; - $form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV5); + private function createBaseFormWithRenderer() + { + $form = new Form(); + $form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV5()); - //horizontal form - $renderer = $form->getRenderer(); - $renderer->setFormHorizontalOrientation(); + //horizontal form + $renderer = $form->getRenderer(); + $renderer->setFormHorizontalOrientation(); - return $form; - } + return $form; + } - private function addInputs(Form $form): Form - { - $form->addEmail('exampleInputEmail1', 'Email address:') - ->setHtmlAttribute('placeholder', 'Enter email') - ->setOption('description', 'We\'ll never share your email with anyone else.'); - $form->addPassword('exampleInputPassword1', 'Password') - ->setHtmlAttribute('placeholder', 'Password'); + private function addInputs(Form $form): Form + { + $form->addEmail('exampleInputEmail1', 'Email address:') + ->setHtmlAttribute('placeholder', 'Enter email') + ->setOption('description', 'We\'ll never share your email with anyone else.'); - $form->addCheckbox('checkbox', 'Check me out'); - $form->addCheckbox('checkbox2', 'Check me out2'); + $form->addPassword('exampleInputPassword1', 'Password') + ->setHtmlAttribute('placeholder', 'Password'); - $form->addRadioList('country', 'Country', [ - 'cz' => 'Česká republika', - 'sk' => 'Slovensko', - 'eu' => 'EU', - ]); + $form->addCheckbox('checkbox', 'Check me out'); + $form->addCheckbox('checkbox2', 'Check me out2'); - $form->addEmail('exampleInputEmail2', 'Email address') - ->setHtmlAttribute('placeholder', 'name@example.com'); + $form->addRadioList('country', 'Country', [ + 'cz' => 'Česká republika', + 'sk' => 'Slovensko', + 'eu' => 'EU', + ]); - $form->addSelect('exampleSelect', 'Example select', [ - '1', - '2', - '3', - '4', - '5',]); + $form->addEmail('exampleInputEmail2', 'Email address') + ->setHtmlAttribute('placeholder', 'name@example.com'); - $form->addMultiSelect('exampleMultipleSelect', 'Example multiple select', [ - '1', - '2', - '3', - '4', - '5',]); + $form->addSelect('exampleSelect', 'Example select', [ + '1', + '2', + '3', + '4', + '5', ]); - $form->addTextArea('textarea', 'Example textarea'); + $form->addMultiSelect('exampleMultipleSelect', 'Example multiple select', [ + '1', + '2', + '3', + '4', + '5', ]); - $form->addUpload('upload', 'Example upload'); + $form->addTextArea('textarea', 'Example textarea'); - $form->addMultiUpload('multiUpload', 'Example multiUpload'); + $form->addUpload('upload', 'Example upload'); - //sizes - $form->addEmail('formControlLg', '.form-control-lg') - ->setHtmlAttribute('class', 'form-control-lg') - ->setHtmlAttribute('placeholder', '.form-control-lg'); + $form->addMultiUpload('multiUpload', 'Example multiUpload'); - $form->addEmail('formControl', '.form-control') - ->setHtmlAttribute('placeholder', 'Default input'); + //sizes + $form->addEmail('formControlLg', '.form-control-lg') + ->setHtmlAttribute('class', 'form-control-lg') + ->setHtmlAttribute('placeholder', '.form-control-lg'); - $form->addEmail('formControlSm', '.form-control-sm') - ->setHtmlAttribute('class', 'form-control-sm') - ->setHtmlAttribute('placeholder', '.form-control-sm'); + $form->addEmail('formControl', '.form-control') + ->setHtmlAttribute('placeholder', 'Default input'); - //sizes select - $form->addSelect('largeSelect', 'Large select', ['Large select']) - ->setHtmlAttribute('class', 'form-control-lg'); + $form->addEmail('formControlSm', '.form-control-sm') + ->setHtmlAttribute('class', 'form-control-sm') + ->setHtmlAttribute('placeholder', '.form-control-sm'); - $form->addSelect('defaultSelect', ' Default select', ['Default select']); + //sizes select + $form->addSelect('largeSelect', 'Large select', ['Large select']) + ->setHtmlAttribute('class', 'form-control-lg'); - $form->addSelect('smallSelect', 'Small select', ['Small select']) - ->setHtmlAttribute('class', 'form-control-sm'); + $form->addSelect('defaultSelect', ' Default select', ['Default select']); - //disables - $form->addText('disabled', 'Disabled:') - ->setHtmlAttribute('placeholder', 'Disabled input here…') - ->setDisabled(true); + $form->addSelect('smallSelect', 'Small select', ['Small select']) + ->setHtmlAttribute('class', 'form-control-sm'); + //disables + $form->addText('disabled', 'Disabled:') + ->setHtmlAttribute('placeholder', 'Disabled input here…') + ->setDisabled(true); - $form->addSubmit('submit', 'Submit')->setHtmlAttribute('class', 'btn btn-primary'); - return $form; - } + $form->addSubmit('submit', 'Submit')->setHtmlAttribute('class', 'btn btn-primary'); + return $form; + } - public function testVerticalCheckInlineForm() - { - $form = $this->createBaseFormWithRenderer(); - $form->addCheckbox('mondayCheckbox', 'Monday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); - $form->addCheckbox('tuesdayCheckbox', 'Tuesday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); - $form->addCheckbox('wednesdayCheckbox', 'Wednesday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); - $form->addCheckbox('thurstdayCheckbox', 'Thurstday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); - $form->addCheckbox('fridayCheckbox', 'Friday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); - $form->addCheckbox('saturdayCheckbox', 'Saturday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); - $form->addCheckbox('sundayCheckbox', 'Sunday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + public function testVerticalCheckInlineForm() + { + $form = $this->createBaseFormWithRenderer(); - $form->addRadioList('weekRadio', 'Week radio', [ - 'monday' => 'Monday', - 'tuesday' => 'Tuesday', - 'wednesday' => 'Wednesday', - 'thurstday' => 'Thurstday', - 'friday' => 'Friday', - 'saturday' => 'Saturday', - 'sunday' => 'Sunday', - ])->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('mondayCheckbox', 'Monday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('tuesdayCheckbox', 'Tuesday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('wednesdayCheckbox', 'Wednesday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('thurstdayCheckbox', 'Thurstday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('fridayCheckbox', 'Friday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('saturdayCheckbox', 'Saturday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + $form->addCheckbox('sundayCheckbox', 'Sunday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); - $html = (string)$form; - - //Assert::same('', (string) $html); + $form->addRadioList('weekRadio', 'Week radio', [ + 'monday' => 'Monday', + 'tuesday' => 'Tuesday', + 'wednesday' => 'Wednesday', + 'thurstday' => 'Thurstday', + 'friday' => 'Friday', + 'saturday' => 'Saturday', + 'sunday' => 'Sunday', + ])->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); - $dom = Tester\DomQuery::fromHtml($html); - - $this->checkInlineCheckbox($dom, 0, 'Monday'); - $this->checkInlineCheckbox($dom, 1, 'Tuesday'); - $this->checkInlineCheckbox($dom, 2, 'Wednesday'); - $this->checkInlineCheckbox($dom, 3, 'Thurstday'); - $this->checkInlineCheckbox($dom, 4, 'Friday'); - $this->checkInlineCheckbox($dom, 5, 'Saturday'); - $this->checkInlineCheckbox($dom, 6, 'Sunday'); - - Assert::same('form-group row', (string)$dom->find('div.form-group')[7]->attributes()['class']); - Assert::same('Week radio', (string)$dom->find('div.form-group.row div.col-sm-3.col-form-label label')[0]); - $this->checkInlineRadio($dom, 7, 'Monday'); - $this->checkInlineRadio($dom, 8, 'Tuesday'); - $this->checkInlineRadio($dom, 9, 'Wednesday'); - $this->checkInlineRadio($dom, 10, 'Thurstday'); - $this->checkInlineRadio($dom, 11, 'Friday'); - $this->checkInlineRadio($dom, 12, 'Saturday'); - } + $html = (string) $form; + + //Assert::same('', (string) $html); + + $dom = Tester\DomQuery::fromHtml($html); + + $this->checkInlineCheckbox($dom, 0, 'Monday'); + $this->checkInlineCheckbox($dom, 1, 'Tuesday'); + $this->checkInlineCheckbox($dom, 2, 'Wednesday'); + $this->checkInlineCheckbox($dom, 3, 'Thurstday'); + $this->checkInlineCheckbox($dom, 4, 'Friday'); + $this->checkInlineCheckbox($dom, 5, 'Saturday'); + $this->checkInlineCheckbox($dom, 6, 'Sunday'); + + Assert::same('form-group row', (string) $dom->find('div.form-group')[7]->attributes()['class']); + Assert::same('Week radio', (string) $dom->find('div.form-group.row div.col-sm-3.col-form-label label')[0]); + $this->checkInlineRadio($dom, 7, 'Monday'); + $this->checkInlineRadio($dom, 8, 'Tuesday'); + $this->checkInlineRadio($dom, 9, 'Wednesday'); + $this->checkInlineRadio($dom, 10, 'Thurstday'); + $this->checkInlineRadio($dom, 11, 'Friday'); + $this->checkInlineRadio($dom, 12, 'Saturday'); + } + + + private function checkInlineCheckbox($dom, $position, $label) + { + Assert::contains('form-group row', (string) $dom->find('div.form-group.row')[$position]->attributes()['class']); + Assert::same($label, (string) $dom->find('div label')[$position]); + Assert::contains('form-check-label', (string) $dom->find('div label')[$position]->attributes()['class']); + Assert::contains('form-check-input', (string) $dom->find('div input')[$position]->attributes()['class']); + } - private function checkInlineCheckbox($dom, $position, $label) - { - Assert::contains('form-group row', (string)$dom->find('div.form-group.row')[$position]->attributes()['class']); - Assert::same($label, (string)$dom->find('div label')[$position]); - Assert::contains('form-check-label', (string)$dom->find('div label')[$position]->attributes()['class']); - Assert::contains('form-check-input', (string)$dom->find('div input')[$position]->attributes()['class']); - } - - - private function checkInlineRadio($dom, $position, $label) - { - Assert::contains($label, (string)$dom->find('div.form-group.row div.col-sm-9 label')[$position]); - Assert::contains('form-check-label', (string)$dom->find('div.form-group.row div.col-sm-9 label')[$position]->attributes()['class']); - Assert::contains('form-check-input', (string)$dom->find('div.form-group.row div.col-sm-9 input')[$position]->attributes()['class']); - } - + private function checkInlineRadio($dom, $position, $label) + { + Assert::contains($label, (string) $dom->find('div.form-group.row div.col-sm-9 label')[$position]); + Assert::contains('form-check-label', (string) $dom->find('div.form-group.row div.col-sm-9 label')[$position]->attributes()['class']); + Assert::contains('form-check-input', (string) $dom->find('div.form-group.row div.col-sm-9 input')[$position]->attributes()['class']); + } } -$test = new BootstrapHorizontalRendererV5; +$test = new BootstrapHorizontalRendererV5(); $test->run(); diff --git a/tests/tests/BoostrapV5/BootstrapVerticalRendererV5.phpt b/tests/tests/BoostrapV5/BootstrapVerticalRendererV5.phpt index efdf44c..147b610 100644 --- a/tests/tests/BoostrapV5/BootstrapVerticalRendererV5.phpt +++ b/tests/tests/BoostrapV5/BootstrapVerticalRendererV5.phpt @@ -15,8 +15,8 @@ class BootstrapVerticalRendererV5 extends Tester\TestCase { private function createBaseFormWithRenderer() { - $form = new Form; - $form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV5); + $form = new Form(); + $form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV5()); return $form; } @@ -215,5 +215,5 @@ class BootstrapVerticalRendererV5 extends Tester\TestCase } } -$test = new BootstrapVerticalRendererV5; +$test = new BootstrapVerticalRendererV5(); $test->run(); From f83920da5af212d08efe7d34c3ba285404c4401e Mon Sep 17 00:00:00 2001 From: vEnCa-X Date: Fri, 29 Dec 2023 19:50:44 +0100 Subject: [PATCH 06/14] CS --- gulpfile.js | 8 ++++---- src/BootstrapRendererV4.php | 4 ++-- src/BootstrapRendererV5.php | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index bdb6dfe..82f3a7b 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -8,10 +8,10 @@ gulp.task('shell_composer_update', shell.task('composer update')); gulp.task('shell_composer_update_prefer_lowest', shell.task('composer update --no-progress --prefer-dist --prefer-lowest --prefer-stable')); gulp.task('shell_test', shell.task('vendor\\bin\\tester tests -s -p php')); -gulp.task('shell_netteCodeChecker', shell.task('php .\\..\\..\\nette-code-checker\\code-checker -d src --short-arrays --strict-types')); -gulp.task('shell_netteCodeCheckerFIX', shell.task('php .\\..\\..\\nette-code-checker\\code-checker -d src --short-arrays --strict-types --fix')); -gulp.task('shell_netteCodingStandard', shell.task('php .\\..\\..\\nette-coding-standard\\ecs check src tests --preset php71')); -gulp.task('shell_netteCodingStandardFIX', shell.task('php .\\..\\..\\nette-coding-standard\\ecs check src tests --preset php71 --fix')); +gulp.task('shell_netteCodeChecker', shell.task('php ..\\..\\nette-code-checker\\code-checker -d src --short-arrays --strict-types')); +gulp.task('shell_netteCodeCheckerFIX', shell.task('php ..\\..\\nette-code-checker\\code-checker -d src --short-arrays --strict-types --fix')); +gulp.task('shell_netteCodingStandard', shell.task('php ..\\..\\nette-coding-standard\\ecs check src tests --preset php81')); +gulp.task('shell_netteCodingStandardFIX', shell.task('php ..\\..\\nette-coding-standard\\ecs check src tests --preset php81 --fix')); gulp.task('shell_phpstan', shell.task('c:\\www\\phpstan-nette\\vendor\\bin\\phpstan.bat analyse src tests --level=0 --memory-limit=4000M')); diff --git a/src/BootstrapRendererV4.php b/src/BootstrapRendererV4.php index 21a5d7c..5e1cf95 100644 --- a/src/BootstrapRendererV4.php +++ b/src/BootstrapRendererV4.php @@ -355,8 +355,8 @@ public function renderControl(Nette\Forms\IControl $control): Html ]), ['for:' => $ids] + $control->getItemLabelPrototype()->attrs, //$control->getSeparatorPrototype() - Html::el('div') - ) + Html::el('div'), + ), ); $el = Html::el('fieldset')->addClass('form-group'); diff --git a/src/BootstrapRendererV5.php b/src/BootstrapRendererV5.php index 2d52691..52f9e46 100644 --- a/src/BootstrapRendererV5.php +++ b/src/BootstrapRendererV5.php @@ -361,8 +361,8 @@ public function renderControl(Nette\Forms\Control $control): Html ]), ['for:' => $ids] + $control->getItemLabelPrototype()->attrs, //$control->getSeparatorPrototype() - Html::el('div') - ) + Html::el('div'), + ), ); $el = Html::el('fieldset')->addClass('form-group'); From 2e1148d26b3ced2db045af74c1fa5f4567f18956 Mon Sep 17 00:00:00 2001 From: vEnCa-X Date: Sat, 30 Dec 2023 09:10:45 +0100 Subject: [PATCH 07/14] zakladni workflows --- .github/workflows/coding-style.yml | 31 ++++++++++++++ .github/workflows/static-analysis.yml | 18 ++++++++ .github/workflows/tests.yml | 60 +++++++++++++++++++++++++++ .gitignore | 1 + gulpfile.js | 4 +- 5 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/coding-style.yml create mode 100644 .github/workflows/static-analysis.yml create mode 100644 .github/workflows/tests.yml diff --git a/.github/workflows/coding-style.yml b/.github/workflows/coding-style.yml new file mode 100644 index 0000000..9be81ee --- /dev/null +++ b/.github/workflows/coding-style.yml @@ -0,0 +1,31 @@ +name: Coding Style + +on: [push, pull_request] + +jobs: + nette_cc: + name: Nette Code Checker + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: shivammathur/setup-php@v2 + with: + php-version: 8.1 + coverage: none + + - run: composer create-project nette/code-checker temp/code-checker ^3 --no-progress + - run: php temp/code-checker/code-checker -d src --strict-types --no-progress --ignore expected + + + nette_cs: + name: Nette Coding Standard + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: shivammathur/setup-php@v2 + with: + php-version: 8.1 + coverage: none + + - run: composer create-project nette/coding-standard temp/coding-standard ^3 --no-progress + - run: php temp/coding-standard/ecs check src tests diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml new file mode 100644 index 0000000..486c8b9 --- /dev/null +++ b/.github/workflows/static-analysis.yml @@ -0,0 +1,18 @@ +name: Static Analysis (only informative) + +on: [push, pull_request] + +jobs: + phpstan: + name: PHPStan + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: shivammathur/setup-php@v2 + with: + php-version: 8.1 + coverage: none + + - run: composer install --no-progress --prefer-dist + - run: composer phpstan + continue-on-error: true # is only informative diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..144b369 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,60 @@ +name: Tests + +on: [push, pull_request] + +jobs: + tests: + runs-on: ubuntu-latest + strategy: + matrix: + php: ['8.1', '8.2', '8.3'] + + fail-fast: false + + name: PHP ${{ matrix.php }} tests + steps: + - uses: actions/checkout@v3 + - uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: none + + - run: composer install --no-progress --prefer-dist + - run: vendor/bin/tester tests -s -C + - if: failure() + uses: actions/upload-artifact@v3 + with: + name: output + path: tests/**/output + + + lowest_dependencies: + name: Lowest Dependencies + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: shivammathur/setup-php@v2 + with: + php-version: 8.1 + coverage: none + + - run: composer update --no-progress --prefer-dist --prefer-lowest --prefer-stable + - run: vendor/bin/tester tests -s -C + + + code_coverage: + name: Code Coverage + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: shivammathur/setup-php@v2 + with: + php-version: 8.1 + coverage: none + + - run: composer install --no-progress --prefer-dist + - run: vendor/bin/tester -p phpdbg tests -s -C --coverage ./coverage.xml --coverage-src ./src + - run: wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.4.3/php-coveralls.phar + - env: + COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: php php-coveralls.phar --verbose --config tests/.coveralls.yml diff --git a/.gitignore b/.gitignore index 3521ce6..f1e233a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /nette-code-checker /nette-coding-standard /tests/tests/output +/package-lock.json diff --git a/gulpfile.js b/gulpfile.js index 82f3a7b..aa9eeba 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -8,8 +8,8 @@ gulp.task('shell_composer_update', shell.task('composer update')); gulp.task('shell_composer_update_prefer_lowest', shell.task('composer update --no-progress --prefer-dist --prefer-lowest --prefer-stable')); gulp.task('shell_test', shell.task('vendor\\bin\\tester tests -s -p php')); -gulp.task('shell_netteCodeChecker', shell.task('php ..\\..\\nette-code-checker\\code-checker -d src --short-arrays --strict-types')); -gulp.task('shell_netteCodeCheckerFIX', shell.task('php ..\\..\\nette-code-checker\\code-checker -d src --short-arrays --strict-types --fix')); +gulp.task('shell_netteCodeChecker', shell.task('php ..\\..\\nette-code-checker\\code-checker -d src --strict-types --no-progress --ignore expected')); +gulp.task('shell_netteCodeCheckerFIX', shell.task('php ..\\..\\nette-code-checker\\code-checker -d src --strict-types --no-progress --ignore expected --fix')); gulp.task('shell_netteCodingStandard', shell.task('php ..\\..\\nette-coding-standard\\ecs check src tests --preset php81')); gulp.task('shell_netteCodingStandardFIX', shell.task('php ..\\..\\nette-coding-standard\\ecs check src tests --preset php81 --fix')); From 9c25a9a19374ac2e92531713080651ca9e3cef58 Mon Sep 17 00:00:00 2001 From: vEnCa-X Date: Sat, 30 Dec 2023 09:13:37 +0100 Subject: [PATCH 08/14] php 8.2 --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 144b369..fa24beb 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -35,7 +35,7 @@ jobs: - uses: actions/checkout@v3 - uses: shivammathur/setup-php@v2 with: - php-version: 8.1 + php-version: 8.2 coverage: none - run: composer update --no-progress --prefer-dist --prefer-lowest --prefer-stable From 9b7c52ff5c0826f39ad0310ca2a7367fc4280218 Mon Sep 17 00:00:00 2001 From: vEnCa-X Date: Sat, 30 Dec 2023 09:14:37 +0100 Subject: [PATCH 09/14] without lowest_dependencies --- .github/workflows/tests.yml | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index fa24beb..373b9f4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -27,21 +27,6 @@ jobs: name: output path: tests/**/output - - lowest_dependencies: - name: Lowest Dependencies - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: shivammathur/setup-php@v2 - with: - php-version: 8.2 - coverage: none - - - run: composer update --no-progress --prefer-dist --prefer-lowest --prefer-stable - - run: vendor/bin/tester tests -s -C - - code_coverage: name: Code Coverage runs-on: ubuntu-latest From 786d9055cdc055d172659aae7f9f0494dbc7a0c5 Mon Sep 17 00:00:00 2001 From: vEnCa-X Date: Sat, 30 Dec 2023 09:21:08 +0100 Subject: [PATCH 10/14] tests badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index abeca59..ee43cf9 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # nette-form-renderer -[![Build Status](https://app.travis-ci.com/venca-x/nette-form-renderer.svg?branch=master)](https://app.travis-ci.com/venca-x/nette-form-renderer) +[![Tests](https://github.com/nette/forms/workflows/Tests/badge.svg?branch=master)](https://github.com/venca-x/nette-form-renderer/actions) [![Coverage Status](https://coveralls.io/repos/github/venca-x/nette-form-renderer/badge.svg?branch=master)](https://coveralls.io/github/venca-x/nette-form-renderer?branch=master) [![Latest Stable Version](https://poser.pugx.org/venca-x/nette-form-renderer/v/stable)](https://packagist.org/packages/venca-x/nette-form-renderer) [![Latest Unstable Version](https://poser.pugx.org/venca-x/nette-form-renderer/v/unstable)](https://packagist.org/packages/venca-x/nette-form-renderer) From 1fdae546df13a7b517ba62e5d310a9f300e2c61e Mon Sep 17 00:00:00 2001 From: vEnCa-X Date: Sat, 30 Dec 2023 09:26:57 +0100 Subject: [PATCH 11/14] improve readme --- README.md | 256 +++++++++++++++++++++++++++++++++++++++++++++++++- composer.json | 2 +- package.json | 2 +- 3 files changed, 256 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ee43cf9..39f8bc5 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,12 @@ [![Total Downloads](https://poser.pugx.org/venca-x/nette-form-renderer/downloads)](https://packagist.org/packages/venca-x/nette-form-renderer) [![License](https://poser.pugx.org/venca-x/nette-form-renderer/license)](https://packagist.org/packages/venca-x/nette-form-renderer) -Form renderer for Nette Forms - TwitterBootstrap v4 +Form renderer for Nette Forms - TwitterBootstrap v5 and v4 ## Installation Install with composer: ``` +composer require venca-x/nette-form-renderer composer require venca-x/nette-form-renderer:dev-master ``` @@ -21,7 +22,258 @@ Types of orientation form * Horizontal * Inline -Default is vertical form orientation. +Vertical form orientation is default. + +# Boostrap v5 rendering + +## Vertical orientation form +```php +$form = new Form; +$form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV5()); + +$form->addEmail('exampleInputEmail1', 'Email address:') + ->setHtmlAttribute('placeholder', 'Enter email') + ->setOption('description', 'We\'ll never share your email with anyone else.'); +$form->addPassword('exampleInputPassword1', 'Password') + ->setHtmlAttribute('placeholder', 'Password'); +$form->addCheckbox('checkbox', 'Check me out'); +$form->addCheckbox('checkbox2', 'Check me out2'); + +$form->addRadioList('country', 'Country', [ + 'cz' => 'Česká republika', + 'sk' => 'Slovensko', + 'eu' => 'EU', +]); + +$form->addEmail('exampleInputEmail2', 'Email address') + ->setHtmlAttribute('placeholder', 'name@example.com'); + +$form->addSelect('exampleSelect', 'Example select', [ + '1', + '2', + '3', + '4', + '5', ]); + +$form->addMultiSelect('exampleMultipleSelect', 'Example multiple select', [ + '1', + '2', + '3', + '4', + '5', ]); + +$form->addTextArea('textarea', 'Example textarea'); + +$form->addUpload('upload', 'Example upload'); + +$form->addMultiUpload('multiUpload', 'Example multiUpload'); + + +//sizes +$form->addEmail('formControlLg', '.form-control-lg') + ->setHtmlAttribute('class', 'form-control-lg') + ->setHtmlAttribute('placeholder', '.form-control-lg'); + +$form->addEmail('formControl', '.form-control') + ->setHtmlAttribute('placeholder', 'Default input'); + +$form->addEmail('formControlSm', '.form-control-sm') + ->setHtmlAttribute('class', 'form-control-sm') + ->setHtmlAttribute('placeholder', '.form-control-sm'); + + +//sizes select +$form->addSelect('largeSelect', 'Large select', ['Large select']) + ->setHtmlAttribute('class', 'form-control-lg'); + +$form->addSelect('defaultSelect', ' Default select', ['Default select']); + +$form->addSelect('smallSelect', 'Small select', ['Small select']) + ->setHtmlAttribute('class', 'form-control-sm'); + + +//disables +$form->addText('disabled', 'Disabled:') + ->setHtmlAttribute('placeholder', 'Disabled input here…') + ->setDisabled(true); + + +$form->addSubmit('submit', 'Submit')->setHtmlAttribute('class', 'btn btn-primary'); + +return $form; +``` + +## Vertical orientation form - radios and checkboxes inline +```php +$form = new Form; +$form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV5()); + +$form->addCheckbox('mondayCheckbox', 'Monday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); +$form->addCheckbox('tuesdayCheckbox', 'Tuesday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); +$form->addCheckbox('wednesdayCheckbox', 'Wednesday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); +$form->addCheckbox('thurstdayCheckbox', 'Thurstday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); +$form->addCheckbox('fridayCheckbox', 'Friday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); +$form->addCheckbox('saturdayCheckbox', 'Saturday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); +$form->addCheckbox('sundayCheckbox', 'Sunday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + +$form->addRadioList('weekRadionline', 'Week radio 2', [ + 'monday' => 'Monday', + 'tuesday' => 'Tuesday', + 'wednesday' => 'Wednesday', + 'thurstday' => 'Thurstday', + 'friday' => 'Friday', + 'saturday' => 'Saturday', + 'sunday' => 'Sunday', +])->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + +$form->addSubmit('submit', 'Submit')->setHtmlAttribute('class', 'btn btn-primary'); + +return $form; +``` + +## Horizontal orientation form +```php +$form = new Form; +$form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV5()); + +//horizontal form +$renderer = $form->getRenderer(); +$renderer->setFormHorizontalOrientation(); + +//$renderer->setFormControlLabelWidth('col-sm-6'); +//$renderer->setFormControlContainerWidth('col-sm-6'); + +$form->addEmail('exampleInputEmail1', 'Email address:') + ->setHtmlAttribute('placeholder', 'Enter email') + ->setOption('description', 'We\'ll never share your email with anyone else.'); +$form->addPassword('exampleInputPassword1', 'Password') + ->setHtmlAttribute('placeholder', 'Password'); +$form->addCheckbox('checkbox', 'Check me out'); +$form->addCheckbox('checkbox2', 'Check me out2'); + +$form->addRadioList('country', 'Country', [ + 'cz' => 'Česká republika', + 'sk' => 'Slovensko', + 'eu' => 'EU', +]); + +$form->addEmail('exampleInputEmail2', 'Email address') + ->setHtmlAttribute('placeholder', 'name@example.com'); + +$form->addSelect('exampleSelect', 'Example select', [ + '1', + '2', + '3', + '4', + '5', ]); + +$form->addMultiSelect('exampleMultipleSelect', 'Example multiple select', [ + '1', + '2', + '3', + '4', + '5', ]); + +$form->addTextArea('textarea', 'Example textarea'); + +$form->addUpload('upload', 'Example upload'); + +$form->addMultiUpload('multiUpload', 'Example multiUpload'); + +//sizes +$form->addEmail('formControlLg', '.form-control-lg') + ->setHtmlAttribute('class', 'form-control-lg') + ->setHtmlAttribute('placeholder', '.form-control-lg'); + +$form->addEmail('formControl', '.form-control') + ->setHtmlAttribute('placeholder', 'Default input'); + +$form->addEmail('formControlSm', '.form-control-sm') + ->setHtmlAttribute('class', 'form-control-sm') + ->setHtmlAttribute('placeholder', '.form-control-sm'); + + +//sizes select +$form->addSelect('largeSelect', 'Large select', ['Large select']) + ->setHtmlAttribute('class', 'form-control-lg'); + +$form->addSelect('defaultSelect', ' Default select', ['Default select']); + +$form->addSelect('smallSelect', 'Small select', ['Small select']) + ->setHtmlAttribute('class', 'form-control-sm'); + + +//disables +$form->addText('disabled', 'Disabled:') + ->setHtmlAttribute('placeholder', 'Disabled input here…') + ->setDisabled(true); + + +$form->addSubmit('submit', 'Submit')->setHtmlAttribute('class', 'btn btn-primary'); + +return $form; +``` + +## Horizontal orientation form - radios and checkboxes inline +**It is not possible to place checkboxes on one line**, only radios inline. +```php +$form = new Form; +$form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV5()); + +//horizontal form +$renderer = $form->getRenderer(); +$renderer->setFormHorizontalOrientation(); + +//$renderer->setFormControlLabelWidth('col-sm-6'); +//$renderer->setFormControlContainerWidth('col-sm-6'); + +//!!!!!!!!! this orientation **don't work** !!!!!!!!!!!!!!!!! +$form->addCheckbox('mondayCheckbox', 'Monday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); +$form->addCheckbox('tuesdayCheckbox', 'Tuesday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); +$form->addCheckbox('wednesdayCheckbox', 'Wednesday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); +$form->addCheckbox('thurstdayCheckbox', 'Thurstday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); +$form->addCheckbox('fridayCheckbox', 'Friday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); +$form->addCheckbox('saturdayCheckbox', 'Saturday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); +$form->addCheckbox('sundayCheckbox', 'Sunday')->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + +$form->addRadioList('weekRadionline', 'Week radio 2', [ + 'monday' => 'Monday', + 'tuesday' => 'Tuesday', + 'wednesday' => 'Wednesday', + 'thurstday' => 'Thurstday', + 'friday' => 'Friday', + 'saturday' => 'Saturday', + 'sunday' => 'Sunday', +])->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV5::FORM_CHECK_INLINE); + +$form->addSubmit('submit', 'Submit')->setHtmlAttribute('class', 'btn btn-primary'); + +return $form; +``` + +## Inline orientation form +```php +$form = new Form; +$form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV5()); + +//inline form +$renderer = $form->getRenderer(); +$renderer->setFormInline(); + +$form->addEmail('loginemail', 'E-mail address:') + ->setHtmlAttribute('placeholder', 'Enter e-mail'); +$form->addPassword('password', 'Password') + ->setHtmlAttribute('placeholder', 'Password'); +$form->addCheckbox('checkbox', 'Check me out'); + +$form->addSubmit('submit', 'Submit')->setHtmlAttribute('class', 'btn btn-primary'); + +return $form; +``` + + + +# Boostrap v4 rendering ## Vertical orientation form ```php diff --git a/composer.json b/composer.json index 23f8c0e..4d44aed 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "venca-x/nette-form-renderer", - "description": "Nette form renderer (Twitter Bootstrap 4)", + "description": "Nette form renderer (Twitter bootstrap v5 and v4)", "license": ["MIT", "BSD-3-Clause", "GPL-2.0", "GPL-3.0"], "authors": [ { diff --git a/package.json b/package.json index 3db0fec..3979bd5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "nette-form-renderer", "version": "1.0.0", - "description": "Nette form renderer (Twitter bootstrap v4)", + "description": "Nette form renderer (Twitter bootstrap v5 and v4)", "main": "gulpfile.js", "directories": { "test": "tests" From 9f1fd5783f59547f43bd76940d6b071bc98686e7 Mon Sep 17 00:00:00 2001 From: vEnCa-X Date: Sat, 30 Dec 2023 09:49:34 +0100 Subject: [PATCH 12/14] improve radio rendering --- src/BootstrapRendererV5.php | 8 +-- .../bootstrap-v5/form-horizontal-sm-6.html | 2 +- .../bootstrap-v5/form-horizontal.html | 2 +- tests/expected/bootstrap-v5/form-radio.html | 69 +++++++++++++++++++ .../expected/bootstrap-v5/form-vertical.html | 2 +- .../BoostrapV5/BootstrapRadioRendererV5.phpt | 53 ++++++++++++++ 6 files changed, 128 insertions(+), 8 deletions(-) create mode 100644 tests/expected/bootstrap-v5/form-radio.html create mode 100644 tests/tests/BoostrapV5/BootstrapRadioRendererV5.phpt diff --git a/src/BootstrapRendererV5.php b/src/BootstrapRendererV5.php index 52f9e46..0dcc2c9 100644 --- a/src/BootstrapRendererV5.php +++ b/src/BootstrapRendererV5.php @@ -41,7 +41,7 @@ public function renderBegin(): string $this->wrappers['controls']['container'] = null; if ($this->isFormVerticalOrientation()) { - $this->wrappers['pair']['container'] = 'div class="mb-3"'; //vertical + $this->wrappers['pair']['container'] = 'div class="' . $this->formVerticalDivMb . '"'; //vertical } else { $this->wrappers['pair']['container'] = 'div class="form-group row"'; //horizontal } @@ -314,7 +314,6 @@ public function renderControl(Nette\Forms\Control $control): Html if ($control instanceof Nette\Forms\Controls\Checkbox) { $control->getLabelPrototype()->addClass('form-check-label'); } else { - $control->getItemLabelPrototype()->addClass('form-check'); $control->getItemLabelPrototype()->addClass('form-check-label'); } $control->getControlPrototype()->addClass('form-check-input'); @@ -360,9 +359,8 @@ public function renderControl(Nette\Forms\Control $control): Html //'data-nette-rules:' => [key($items) => $input->attrs['data-nette-rules']], ]), ['for:' => $ids] + $control->getItemLabelPrototype()->attrs, - //$control->getSeparatorPrototype() - Html::el('div'), - ), + $this->getWrapper('control checkbox') + ) ); $el = Html::el('fieldset')->addClass('form-group'); diff --git a/tests/expected/bootstrap-v5/form-horizontal-sm-6.html b/tests/expected/bootstrap-v5/form-horizontal-sm-6.html index 412d414..8562a46 100644 --- a/tests/expected/bootstrap-v5/form-horizontal-sm-6.html +++ b/tests/expected/bootstrap-v5/form-horizontal-sm-6.html @@ -28,7 +28,7 @@
-
+
diff --git a/tests/expected/bootstrap-v5/form-horizontal.html b/tests/expected/bootstrap-v5/form-horizontal.html index 9ac03f1..ca467f6 100644 --- a/tests/expected/bootstrap-v5/form-horizontal.html +++ b/tests/expected/bootstrap-v5/form-horizontal.html @@ -28,7 +28,7 @@
-
+
diff --git a/tests/expected/bootstrap-v5/form-radio.html b/tests/expected/bootstrap-v5/form-radio.html new file mode 100644 index 0000000..3b6f639 --- /dev/null +++ b/tests/expected/bootstrap-v5/form-radio.html @@ -0,0 +1,69 @@ +
+ + +
+ + +
+
+ + +
+
+ + + + +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+
+
+ + + + + + + + + +
\ No newline at end of file diff --git a/tests/expected/bootstrap-v5/form-vertical.html b/tests/expected/bootstrap-v5/form-vertical.html index 44af5b6..8154039 100644 --- a/tests/expected/bootstrap-v5/form-vertical.html +++ b/tests/expected/bootstrap-v5/form-vertical.html @@ -28,7 +28,7 @@
-
+
diff --git a/tests/tests/BoostrapV5/BootstrapRadioRendererV5.phpt b/tests/tests/BoostrapV5/BootstrapRadioRendererV5.phpt new file mode 100644 index 0000000..71b8415 --- /dev/null +++ b/tests/tests/BoostrapV5/BootstrapRadioRendererV5.phpt @@ -0,0 +1,53 @@ +createBaseFormWithRenderer(); + + $form->addRadioList('country', 'Country', [ + 'cz' => 'Česká republika', + 'sk' => 'Slovensko', + 'eu' => 'EU', + ]); + + $form->addRadioList('weekRadionline', 'Week radio 2', [ + 'monday' => 'Monday', + 'tuesday' => 'Tuesday', + 'wednesday' => 'Wednesday', + 'thurstday' => 'Thurstday', + 'friday' => 'Friday', + 'saturday' => 'Saturday', + 'sunday' => 'Sunday', + ])->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV4::FORM_CHECK_INLINE); + + $form->addSubmit('submit', 'Submit')->setHtmlAttribute('class', 'btn btn-primary'); + + $html = (string) $form; + + Assert::matchFile(__DIR__ . '/../../expected/bootstrap-v5/form-radio.html', $html); + } + + + private function createBaseFormWithRenderer() + { + $form = new Form(); + $form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV5()); + return $form; + } +} + +$test = new BootstrapRadioRendererV5(); +$test->run(); From 0985676141ecfa11ea42a8b7736c7d0bd89387d6 Mon Sep 17 00:00:00 2001 From: vEnCa-X Date: Sat, 30 Dec 2023 09:50:37 +0100 Subject: [PATCH 13/14] remove deatch code --- src/BootstrapRendererV5.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/BootstrapRendererV5.php b/src/BootstrapRendererV5.php index 0dcc2c9..9998e7a 100644 --- a/src/BootstrapRendererV5.php +++ b/src/BootstrapRendererV5.php @@ -195,7 +195,6 @@ public function renderPair(Nette\Forms\Control $control): string $pair = $this->getWrapper('pair container'); } else { $pair = $this->getWrapper('pair container'); - //@TODO how to set many checkboxes on same line? problem... } } @@ -342,12 +341,6 @@ public function renderControl(Nette\Forms\Control $control): Html $ids = []; $values = []; - if ($control->generateId) { - foreach ($items as $value => $label) { - $ids[$value] = $input->id . '-' . $value; - $values[$value] = $value; - } - } $elControl = $control->getContainerPrototype()->setHtml( Nette\Forms\Helpers::createInputList( $control->translate($items), From 6c44d14c7e7b360c0649557dcc4ca1e043b63775 Mon Sep 17 00:00:00 2001 From: vEnCa-X Date: Sat, 30 Dec 2023 09:54:40 +0100 Subject: [PATCH 14/14] cs --- src/BootstrapRendererV5.php | 4 +- .../BoostrapV5/BootstrapRadioRendererV5.phpt | 42 +++++++++---------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/BootstrapRendererV5.php b/src/BootstrapRendererV5.php index 9998e7a..7d29a21 100644 --- a/src/BootstrapRendererV5.php +++ b/src/BootstrapRendererV5.php @@ -352,8 +352,8 @@ public function renderControl(Nette\Forms\Control $control): Html //'data-nette-rules:' => [key($items) => $input->attrs['data-nette-rules']], ]), ['for:' => $ids] + $control->getItemLabelPrototype()->attrs, - $this->getWrapper('control checkbox') - ) + $this->getWrapper('control checkbox'), + ), ); $el = Html::el('fieldset')->addClass('form-group'); diff --git a/tests/tests/BoostrapV5/BootstrapRadioRendererV5.phpt b/tests/tests/BoostrapV5/BootstrapRadioRendererV5.phpt index 71b8415..cca2a16 100644 --- a/tests/tests/BoostrapV5/BootstrapRadioRendererV5.phpt +++ b/tests/tests/BoostrapV5/BootstrapRadioRendererV5.phpt @@ -17,21 +17,21 @@ class BootstrapRadioRendererV5 extends Tester\TestCase { $form = $this->createBaseFormWithRenderer(); - $form->addRadioList('country', 'Country', [ - 'cz' => 'Česká republika', - 'sk' => 'Slovensko', - 'eu' => 'EU', - ]); - - $form->addRadioList('weekRadionline', 'Week radio 2', [ - 'monday' => 'Monday', - 'tuesday' => 'Tuesday', - 'wednesday' => 'Wednesday', - 'thurstday' => 'Thurstday', - 'friday' => 'Friday', - 'saturday' => 'Saturday', - 'sunday' => 'Sunday', - ])->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV4::FORM_CHECK_INLINE); + $form->addRadioList('country', 'Country', [ + 'cz' => 'Česká republika', + 'sk' => 'Slovensko', + 'eu' => 'EU', + ]); + + $form->addRadioList('weekRadionline', 'Week radio 2', [ + 'monday' => 'Monday', + 'tuesday' => 'Tuesday', + 'wednesday' => 'Wednesday', + 'thurstday' => 'Thurstday', + 'friday' => 'Friday', + 'saturday' => 'Saturday', + 'sunday' => 'Sunday', + ])->setOption('orientation', VencaX\NetteFormRenderer\BootstrapRendererV4::FORM_CHECK_INLINE); $form->addSubmit('submit', 'Submit')->setHtmlAttribute('class', 'btn btn-primary'); @@ -41,12 +41,12 @@ class BootstrapRadioRendererV5 extends Tester\TestCase } - private function createBaseFormWithRenderer() - { - $form = new Form(); - $form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV5()); - return $form; - } + private function createBaseFormWithRenderer() + { + $form = new Form(); + $form->setRenderer(new VencaX\NetteFormRenderer\BootstrapRendererV5()); + return $form; + } } $test = new BootstrapRadioRendererV5();