Skip to content

Commit b39edba

Browse files
committed
TextBase: added setMaxLength()
1 parent fa8b26b commit b39edba

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/Forms/Controls/TextBase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ public function getEmptyValue()
8686
}
8787

8888

89+
/**
90+
* Sets the maximum number of allowed characters.
91+
* @param int
92+
* @return self
93+
*/
94+
public function setMaxLength($length)
95+
{
96+
$this->control->maxlength = $length;
97+
return $this;
98+
}
99+
100+
89101
/**
90102
* Appends input string filter callback.
91103
* @param callable

tests/Forms/Controls.TextInput.render.phpt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,17 @@ test(function() { // conditional required
9999

100100
test(function() { // maxlength without validation rule
101101
$form = new Form;
102-
$input = $form->addText('text', NULL, NULL, 30);
102+
$input = $form->addText('text')
103+
->setMaxLength(30);
103104

104105
Assert::same('<input type="text" name="text" maxlength="30" id="frm-text">', (string) $input->getControl());
105106
});
106107

107108

108109
test(function() { // validation rule LENGTH
109110
$form = new Form;
110-
$input = $form->addText('text', NULL, NULL, 30)
111+
$input = $form->addText('text')
112+
->setMaxLength(30)
111113
->addRule($form::LENGTH, NULL, array(10, 20));
112114

113115
Assert::same('<input type="text" name="text" maxlength="20" id="frm-text" data-nette-rules=\'[{"op":":length","msg":"Please enter a value between 10 and 20 characters long.","arg":[10,20]}]\'>', (string) $input->getControl());

0 commit comments

Comments
 (0)