Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions src/Traits/BootstrapContainerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -313,32 +313,28 @@ 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;
}

/**
* @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
Expand Down
48 changes: 48 additions & 0 deletions tests/Traits/BootstrapContainerTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading