Skip to content

Commit 61832a5

Browse files
chemixdg
authored andcommitted
TextBase: fixed setEmptyValue() and space at the end of the string [Closes #35]
1 parent 9af766b commit 61832a5

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

src/Forms/Controls/TextBase.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
namespace Nette\Forms\Controls;
99

1010
use Nette,
11-
Nette\Forms\Form;
11+
Nette\Forms\Form,
12+
Nette\Utils\Strings;
1213

1314

1415
/**
@@ -55,12 +56,12 @@ public function getValue()
5556
{
5657
$value = $this->value;
5758
if (!empty($this->control->maxlength)) {
58-
$value = Nette\Utils\Strings::substring($value, 0, $this->control->maxlength);
59+
$value = Strings::substring($value, 0, $this->control->maxlength);
5960
}
6061
foreach ($this->filters as $filter) {
6162
$value = (string) call_user_func($filter, $value);
6263
}
63-
return $value === $this->translate($this->emptyValue) ? '' : $value;
64+
return $value === Strings::trim($this->translate($this->emptyValue)) ? '' : $value;
6465
}
6566

6667

@@ -115,7 +116,7 @@ public function getControl()
115116
{
116117
$el = parent::getControl();
117118
if ($this->emptyValue !== '') {
118-
$el->attrs['data-nette-empty-value'] = $this->translate($this->emptyValue);
119+
$el->attrs['data-nette-empty-value'] = Strings::trim($this->translate($this->emptyValue));
119120
}
120121
if (isset($el->placeholder)) {
121122
$el->placeholder = $this->translate($el->placeholder);

tests/Forms/Controls.TextBase.loadData.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ test(function() { // empty value
4949
});
5050

5151

52+
test(function() { // empty value
53+
$_POST = array('phone' => '+420 ');
54+
55+
$form = new Form;
56+
$input = $form->addText('phone')
57+
->setEmptyValue('+420 ');
58+
59+
Assert::same( '', $input->getValue() );
60+
});
61+
62+
5263
test(function() { // invalid UTF
5364
$_POST = array('invalidutf' => "invalid\xAA\xAA\xAAutf");
5465

tests/Forms/Controls.TextInput.render.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ test(function() { // validation rule RANGE with setType
157157
test(function() { // setEmptyValue
158158
$form = new Form;
159159
$input = $form->addText('text')
160-
->setEmptyValue('empty');
160+
->setEmptyValue('empty ');
161161

162-
Assert::same('<input type="text" name="text" id="frm-text" data-nette-empty-value="empty" value="empty">', (string) $input->getControl());
162+
Assert::same('<input type="text" name="text" id="frm-text" data-nette-empty-value="empty" value="empty ">', (string) $input->getControl());
163163
});
164164

165165

0 commit comments

Comments
 (0)