File tree Expand file tree Collapse file tree 3 files changed +47
-1
lines changed Expand file tree Collapse file tree 3 files changed +47
-1
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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+ });
Original file line number Diff line number Diff 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+
166186test (function () { // setDefaultValue
167187 $ form = new Form ;
168188 $ input = $ form ->addText ('text ' )
You can’t perform that action at this time.
0 commit comments