diff --git a/src/Traits/BootstrapContainerTrait.php b/src/Traits/BootstrapContainerTrait.php index f433318..89096ee 100644 --- a/src/Traits/BootstrapContainerTrait.php +++ b/src/Traits/BootstrapContainerTrait.php @@ -191,7 +191,7 @@ public function addEmail( int $maxLength = 255 ): NetteTextInput { - return $this->addText($name, $label) + return $this->addText($name, $label, null, $maxLength) ->setNullable(BootstrapForm::$allwaysUseNullable) ->addRule(Form::Email); } @@ -313,23 +313,19 @@ public function addSubmit(string $name, $caption = null, ?Closure $onSubmit = nu /** * @param string|Html|null $label - * @param int|null $cols ignored - * @param int|null $maxLength ignored + * @param int|null $cols width of the input, rendered as the cols attribute + * @param int|null $maxLength maximum length of the value, rendered as the maxlength attribute * @return TextInput */ public function addText(string $name, $label = null, ?int $cols = null, ?int $maxLength = null): NetteTextInput { - $comp = new TextInput($label); + $comp = new TextInput($label, $maxLength); $comp->setNullable(BootstrapForm::$allwaysUseNullable); if ($cols !== null) { $comp->setHtmlAttribute('cols', $cols); } - if ($maxLength !== null) { - $comp->setHtmlAttribute('maxlength', $cols); - } - $this->addComponent($comp, $name); return $comp; @@ -337,8 +333,8 @@ public function addText(string $name, $label = null, ?int $cols = null, ?int $ma /** * @param string|Html|null $label - * @param int|null $cols ignored - * @param int|null $rows ignored + * @param int|null $cols width of the textarea, rendered as the cols attribute + * @param int|null $rows height of the textarea, rendered as the rows attribute * @return TextAreaInput */ public function addTextArea(string $name, $label = null, ?int $cols = null, ?int $rows = null): TextArea diff --git a/tests/Traits/BootstrapContainerTraitTest.php b/tests/Traits/BootstrapContainerTraitTest.php index 5c57675..f781856 100644 --- a/tests/Traits/BootstrapContainerTraitTest.php +++ b/tests/Traits/BootstrapContainerTraitTest.php @@ -80,6 +80,54 @@ public function testAddPasswordHidesWhatIsTyped(): void $this->assertStringContainsString('form-control', (string) $input->getControl()); } + /** + * The maxlength attribute has to come from $maxLength, not from the + * visual width in $cols. See issue #104. + */ + public function testAddTextTakesMaxLengthFromItsOwnArgument(): void + { + $form = new BootstrapForm(); + $input = $form->addText('name', 'Name', 10, 50); + + $html = (string) $input->getControl(); + $this->assertStringContainsString('maxlength="50"', $html); + $this->assertStringContainsString('cols="10"', $html); + } + + public function testAddTextRendersMaxLengthWithoutCols(): void + { + $form = new BootstrapForm(); + $input = $form->addText('name', 'Name', null, 50); + + $html = (string) $input->getControl(); + $this->assertStringContainsString('maxlength="50"', $html); + $this->assertStringNotContainsString('cols=', $html); + } + + public function testAddTextWithoutMaxLengthRendersNoMaxLength(): void + { + $form = new BootstrapForm(); + $input = $form->addText('name', 'Name', 10); + + $this->assertStringNotContainsString('maxlength', (string) $input->getControl()); + } + + public function testAddPasswordPassesMaxLengthOn(): void + { + $form = new BootstrapForm(); + $input = $form->addPassword('secret', 'Secret', 10, 20); + + $this->assertStringContainsString('maxlength="20"', (string) $input->getControl()); + } + + public function testAddEmailAppliesItsMaxLength(): void + { + $form = new BootstrapForm(); + + $this->assertStringContainsString('maxlength="255"', (string) $form->addEmail('a', 'A')->getControl()); + $this->assertStringContainsString('maxlength="30"', (string) $form->addEmail('b', 'B', 30)->getControl()); + } + public function testAddMultiUploadTakesSeveralFiles(): void { $form = new BootstrapForm();