From 26f9b27bba31ce13fc6395eacbb09f925bf6f890 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Ot=C3=A1hal?= Date: Fri, 20 Apr 2018 16:44:18 +0200 Subject: [PATCH 1/2] TextBase support mixed as value --- src/Forms/Controls/TextBase.php | 3 +- src/Forms/ScalarValue.php | 43 +++++++++++++++++++++++++++ tests/Forms/Controls.BaseControl.phpt | 28 +++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/Forms/ScalarValue.php diff --git a/src/Forms/Controls/TextBase.php b/src/Forms/Controls/TextBase.php index 175a8ac74..e07f54736 100644 --- a/src/Forms/Controls/TextBase.php +++ b/src/Forms/Controls/TextBase.php @@ -11,6 +11,7 @@ use Nette; use Nette\Forms\Form; +use Nette\Forms\ScalarValue; use Nette\Utils\Strings; @@ -41,7 +42,7 @@ public function setValue($value) } elseif (!is_scalar($value) && !method_exists($value, '__toString')) { throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", gettype($value), $this->name)); } - $this->value = $value; + $this->value = $value instanceof ScalarValue ? $value->getMixedValue() : $value; $this->rawValue = (string) $value; return $this; } diff --git a/src/Forms/ScalarValue.php b/src/Forms/ScalarValue.php new file mode 100644 index 000000000..57c1e0869 --- /dev/null +++ b/src/Forms/ScalarValue.php @@ -0,0 +1,43 @@ +scalarValue = $scalarValue; + $this->mixedValue = $mixedValue; + } + + + /** + * @return mixed + */ + public function getMixedValue() + { + return $this->mixedValue; + } + + + public function __toString(): string + { + return $this->scalarValue; + } +} diff --git a/tests/Forms/Controls.BaseControl.phpt b/tests/Forms/Controls.BaseControl.phpt index e6b5b850b..cb2d18608 100644 --- a/tests/Forms/Controls.BaseControl.phpt +++ b/tests/Forms/Controls.BaseControl.phpt @@ -7,6 +7,7 @@ declare(strict_types=1); use Nette\Forms\Form; +use Nette\Forms\ScalarValue; use Nette\Forms\Validator; use Tester\Assert; @@ -145,3 +146,30 @@ test(function () { // disabled & submitted Assert::same('default', $input->getValue()); }); + + +test(function () { // scalarValue + $form = new Form; + $input = $form->addText('text'); + $input->setValue(new ScalarValue( 'scalarValue', null)); + + Assert::null($input->getValue()); + Assert::same('', (string) $input->getControl()); +}); + + +test(function () { // scalarValue from filter + $form = new Form; + + $input = $form->addText('text'); + $input->setValue('scalarValue'); + $input->addFilter(function (string $scalarValue): ScalarValue { + return new ScalarValue($scalarValue, 'mixedValue'); + }); + + $form->validate(); + + Assert::same('mixedValue', $input->getValue()); + Assert::same('', (string) $input->getControl()); + Assert::true(Validator::validateFilled($input)); +}); From 955641d43e1e1f200f94527138dcbffba8dbbac0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Ot=C3=A1hal?= Date: Fri, 20 Apr 2018 20:38:51 +0200 Subject: [PATCH 2/2] fixup! TextBase support mixed as value --- tests/Forms/Controls.BaseControl.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Forms/Controls.BaseControl.phpt b/tests/Forms/Controls.BaseControl.phpt index cb2d18608..4cef88fd0 100644 --- a/tests/Forms/Controls.BaseControl.phpt +++ b/tests/Forms/Controls.BaseControl.phpt @@ -151,7 +151,7 @@ test(function () { // disabled & submitted test(function () { // scalarValue $form = new Form; $input = $form->addText('text'); - $input->setValue(new ScalarValue( 'scalarValue', null)); + $input->setValue(new ScalarValue('scalarValue', null)); Assert::null($input->getValue()); Assert::same('', (string) $input->getControl());