Skip to content

Commit 98b743f

Browse files
committed
TextBase: added setNullable()
1 parent f6ba8bc commit 98b743f

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

src/Forms/Controls/TextBase.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ abstract class TextBase extends BaseControl
2323
/** @var mixed unfiltered submitted value */
2424
protected $rawValue = '';
2525

26+
/** @var bool */
27+
private $nullable;
28+
2629

2730
/**
2831
* Sets control's value.
@@ -48,7 +51,19 @@ public function setValue($value)
4851
*/
4952
public function getValue()
5053
{
51-
return $this->value;
54+
return $this->nullable && $this->value === '' ? NULL : $this->value;
55+
}
56+
57+
58+
/**
59+
* Sets whether getValue() returns NULL instead of empty string.
60+
* @param bool
61+
* @return self
62+
*/
63+
public function setNullable($value = TRUE)
64+
{
65+
$this->nullable = (bool) $value;
66+
return $this;
5267
}
5368

5469

tests/Forms/Controls.TextArea.render.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,14 @@ test(function () { // setEmptyValue
106106

107107
Assert::same('<textarea name="text" id="frm-text" data-nette-empty-value="empty">empty </textarea>', (string) $input->getControl());
108108
});
109+
110+
111+
test(function () { // setEmptyValue & setNullable
112+
$form = new Form;
113+
$input = $form->addTextArea('text')
114+
->setEmptyValue('empty ')
115+
->setNullable();
116+
117+
Assert::null($input->getValue());
118+
Assert::same('<textarea name="text" id="frm-text" data-nette-empty-value="empty">empty </textarea>', (string) $input->getControl());
119+
});

tests/Forms/Controls.TextInput.render.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,26 @@ test(function () { // setEmptyValue
163163
});
164164

165165

166+
test(function () { // setNullable
167+
$form = new Form;
168+
$input = $form->addText('text')
169+
->setNullable();
170+
171+
Assert::null($input->getValue());
172+
});
173+
174+
175+
test(function () { // setEmptyValue & setNullable
176+
$form = new Form;
177+
$input = $form->addText('text')
178+
->setEmptyValue('empty ')
179+
->setNullable();
180+
181+
Assert::null($input->getValue());
182+
Assert::same('<input type="text" name="text" id="frm-text" data-nette-empty-value="empty" value="empty ">', (string) $input->getControl());
183+
});
184+
185+
166186
test(function () { // setDefaultValue
167187
$form = new Form;
168188
$input = $form->addText('text')

0 commit comments

Comments
 (0)