From ff0d77224770daa77dc45e1bccf67892d9706362 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 30 Jun 2016 14:03:01 +0200 Subject: [PATCH] =?UTF-8?q?IControl::setValue()=20=E2=87=92=20setCurrentVa?= =?UTF-8?q?lues()=20and=20Container::setValues()=20=E2=87=92=20setCurrentV?= =?UTF-8?q?alues()=20[Closes=20#114]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/custom-control.php | 2 +- src/Forms/Container.php | 28 +++++++---- src/Forms/Controls/BaseControl.php | 22 +++++--- src/Forms/Controls/Checkbox.php | 2 +- src/Forms/Controls/ChoiceControl.php | 2 +- src/Forms/Controls/CsrfProtection.php | 2 +- src/Forms/Controls/HiddenField.php | 2 +- src/Forms/Controls/MultiChoiceControl.php | 2 +- src/Forms/Controls/TextBase.php | 2 +- src/Forms/Controls/TextInput.php | 2 +- src/Forms/Controls/UploadControl.php | 2 +- src/Forms/IControl.php | 2 +- src/Forms/Rules.php | 2 +- src/Forms/Validator.php | 6 +-- tests/Forms.Latte/FormMacros.forms.phpt | 2 +- tests/Forms/Container.validate().phpt | 2 +- tests/Forms/Controls.BaseControl.phpt | 4 +- tests/Forms/Controls.Checkbox.loadData.phpt | 8 +-- tests/Forms/Controls.Checkbox.render.phpt | 2 +- .../Forms/Controls.CheckboxList.loadData.phpt | 10 ++-- tests/Forms/Controls.CheckboxList.render.phpt | 2 +- .../Controls.ChoiceControl.loadData.phpt | 14 +++--- .../Controls.CsrfProtection.breachAttack.phpt | 6 +-- tests/Forms/Controls.CsrfProtection.phpt | 10 ++-- .../Forms/Controls.HiddenField.loadData.phpt | 12 ++--- .../Controls.MultiChoiceControl.loadData.phpt | 25 +++++----- .../Controls.MultiSelectBox.loadData.phpt | 10 ++-- .../Forms/Controls.MultiSelectBox.render.phpt | 4 +- tests/Forms/Controls.RadioList.loadData.phpt | 10 ++-- tests/Forms/Controls.RadioList.render.phpt | 2 +- tests/Forms/Controls.SelectBox.isOk.phpt | 4 +- tests/Forms/Controls.SelectBox.loadData.phpt | 10 ++-- tests/Forms/Controls.SelectBox.render.phpt | 4 +- tests/Forms/Controls.TestBase.validators.phpt | 50 +++++++++---------- tests/Forms/Controls.TextArea.render.phpt | 4 +- tests/Forms/Controls.TextBase.loadData.phpt | 10 ++-- tests/Forms/Controls.TextInput.render.phpt | 6 +-- tests/Forms/Forms.userValidator.phpt | 2 +- tests/Forms/Rules.dynamic.phpt | 2 +- tests/Forms/Rules.valid.phpt | 4 +- 40 files changed, 158 insertions(+), 139 deletions(-) diff --git a/examples/custom-control.php b/examples/custom-control.php index 4967406b8..f111aa9e7 100644 --- a/examples/custom-control.php +++ b/examples/custom-control.php @@ -34,7 +34,7 @@ public function __construct($label = null) } - public function setValue($value) + public function setCurrentValue($value) { if ($value === null) { $this->day = $this->month = $this->year = ''; diff --git a/src/Forms/Container.php b/src/Forms/Container.php index 700ce9b2f..bc82aff49 100644 --- a/src/Forms/Container.php +++ b/src/Forms/Container.php @@ -17,16 +17,16 @@ * * @property Nette\Utils\ArrayHash $values * @property-read \Iterator $controls - * @property-read Form|null $form + * @property-read Form|NULL $form */ class Container extends Nette\ComponentModel\Container implements \ArrayAccess { + /** @var callable[] function (Container $sender); Occurs when the form is validated */ public $onValidate; - /** @var ControlGroup|null */ + /** @var ControlGroup|NULL */ protected $currentGroup; - /** @var callable[] extension methods */ private static $extMethods = []; @@ -45,7 +45,7 @@ public function setDefaults(iterable $values, bool $erase = false) { $form = $this->getForm(false); if (!$form || !$form->isAnchored() || !$form->isSubmitted()) { - $this->setValues($values, $erase); + $this->setCurrentValues($values, $erase); } return $this; } @@ -56,7 +56,7 @@ public function setDefaults(iterable $values, bool $erase = false) * @return static * @internal */ - public function setValues(iterable $values, bool $erase = false) + public function setCurrentValues(iterable $values, bool $erase = false) { if ($values instanceof \Traversable) { $values = iterator_to_array($values); @@ -68,18 +68,18 @@ public function setValues(iterable $values, bool $erase = false) foreach ($this->getComponents() as $name => $control) { if ($control instanceof IControl) { if (array_key_exists($name, $values)) { - $control->setValue($values[$name]); + $control->setCurrentValue($values[$name]); } elseif ($erase) { - $control->setValue(null); + $control->setCurrentValue(null); } } elseif ($control instanceof self) { if (array_key_exists($name, $values)) { - $control->setValues($values[$name], $erase); + $control->setCurrentValues($values[$name], $erase); } elseif ($erase) { - $control->setValues([], $erase); + $control->setCurrentValues([], $erase); } } } @@ -87,6 +87,16 @@ public function setValues(iterable $values, bool $erase = false) } + /** + * @deprecated + */ + public function setValues($values, $erase = false) + { + trigger_error(__METHOD__ . '() is deprecated; use setCurrentValues() instead.', E_USER_DEPRECATED); + return $this->setCurrentValues($values, $erase); + } + + /** * Returns the values submitted by the form. * @return Nette\Utils\ArrayHash|array diff --git a/src/Forms/Controls/BaseControl.php b/src/Forms/Controls/BaseControl.php index 41ca3c860..57e926ec6 100644 --- a/src/Forms/Controls/BaseControl.php +++ b/src/Forms/Controls/BaseControl.php @@ -62,7 +62,7 @@ abstract class BaseControl extends Nette\ComponentModel\Component implements ICo /** @var array */ private $errors = []; - /** @var bool|null */ + /** @var bool|NULL */ private $omitted; /** @var Rules */ @@ -92,7 +92,7 @@ public function __construct($caption = null) if (self::$autoOptional) { $this->setRequired(false); } - $this->setValue(null); + $this->setCurrentValue(null); } @@ -142,7 +142,7 @@ public function getForm(bool $throw = true): ?Form */ public function loadHttpData(): void { - $this->setValue($this->getHttpData(Form::DATA_TEXT)); + $this->setCurrentValue($this->getHttpData(Form::DATA_TEXT)); } @@ -173,10 +173,18 @@ public function getHtmlName(): string * @return static * @internal */ + public function setCurrentValue($value) + { + return $this->setValue($value); + } + + + /** + * @deprecated + */ public function setValue($value) { $this->value = $value; - return $this; } @@ -208,7 +216,7 @@ public function setDefaultValue($value) { $form = $this->getForm(false); if ($this->isDisabled() || !$form || !$form->isAnchored() || !$form->isSubmitted()) { - $this->setValue($value); + $this->setCurrentValue($value); } return $this; } @@ -221,7 +229,7 @@ public function setDefaultValue($value) public function setDisabled($value = true) { if ($this->disabled = (bool) $value) { - $this->setValue(null); + $this->setCurrentValue(null); } elseif (($form = $this->getForm(false)) && $form->isAnchored() && $form->isSubmitted()) { $this->loadHttpData(); } @@ -327,7 +335,7 @@ public function getLabelPrototype(): Html /** * Changes control's HTML id. - * @param mixed new ID, or false or null + * @param mixed new ID, or FALSE or NULL * @return static */ public function setHtmlId($id) diff --git a/src/Forms/Controls/Checkbox.php b/src/Forms/Controls/Checkbox.php index 242fc9f4b..b72b1aab2 100644 --- a/src/Forms/Controls/Checkbox.php +++ b/src/Forms/Controls/Checkbox.php @@ -39,7 +39,7 @@ public function __construct($label = null) * @return static * @internal */ - public function setValue($value) + public function setCurrentValue($value) { if (!is_scalar($value) && $value !== null) { throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", gettype($value), $this->name)); diff --git a/src/Forms/Controls/ChoiceControl.php b/src/Forms/Controls/ChoiceControl.php index 57a921e56..1f86c9bdc 100644 --- a/src/Forms/Controls/ChoiceControl.php +++ b/src/Forms/Controls/ChoiceControl.php @@ -61,7 +61,7 @@ public function loadHttpData(): void * @return static * @internal */ - public function setValue($value) + public function setCurrentValue($value) { if ($this->checkAllowedValues && $value !== null && !array_key_exists((string) $value, $this->items)) { $set = Nette\Utils\Strings::truncate(implode(', ', array_map(function ($s) { return var_export($s, true); }, array_keys($this->items))), 70, '...'); diff --git a/src/Forms/Controls/CsrfProtection.php b/src/Forms/Controls/CsrfProtection.php index 24c830f97..17ef4a057 100644 --- a/src/Forms/Controls/CsrfProtection.php +++ b/src/Forms/Controls/CsrfProtection.php @@ -49,7 +49,7 @@ protected function attached(Nette\ComponentModel\IComponent $parent): void * @return static * @internal */ - public function setValue($value) + public function setCurrentValue($value) { return $this; } diff --git a/src/Forms/Controls/HiddenField.php b/src/Forms/Controls/HiddenField.php index 320244886..58d0ef686 100644 --- a/src/Forms/Controls/HiddenField.php +++ b/src/Forms/Controls/HiddenField.php @@ -39,7 +39,7 @@ public function __construct($persistentValue = null) * @return static * @internal */ - public function setValue($value) + public function setCurrentValue($value) { if (!is_scalar($value) && $value !== null && !method_exists($value, '__toString')) { throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", gettype($value), $this->name)); diff --git a/src/Forms/Controls/MultiChoiceControl.php b/src/Forms/Controls/MultiChoiceControl.php index 527fe26f7..a410363d5 100644 --- a/src/Forms/Controls/MultiChoiceControl.php +++ b/src/Forms/Controls/MultiChoiceControl.php @@ -56,7 +56,7 @@ public function loadHttpData(): void * @return static * @internal */ - public function setValue($values) + public function setCurrentValue($values) { if (is_scalar($values) || $values === null) { $values = (array) $values; diff --git a/src/Forms/Controls/TextBase.php b/src/Forms/Controls/TextBase.php index 175a8ac74..f2863ff53 100644 --- a/src/Forms/Controls/TextBase.php +++ b/src/Forms/Controls/TextBase.php @@ -34,7 +34,7 @@ abstract class TextBase extends BaseControl * @return static * @internal */ - public function setValue($value) + public function setCurrentValue($value) { if ($value === null) { $value = ''; diff --git a/src/Forms/Controls/TextInput.php b/src/Forms/Controls/TextInput.php index 3f033e4ea..5bad14b19 100644 --- a/src/Forms/Controls/TextInput.php +++ b/src/Forms/Controls/TextInput.php @@ -35,7 +35,7 @@ public function __construct($label = null, int $maxLength = null) */ public function loadHttpData(): void { - $this->setValue($this->getHttpData(Form::DATA_LINE)); + $this->setCurrentValue($this->getHttpData(Form::DATA_LINE)); } diff --git a/src/Forms/Controls/UploadControl.php b/src/Forms/Controls/UploadControl.php index 154f5c139..1d2e533e4 100644 --- a/src/Forms/Controls/UploadControl.php +++ b/src/Forms/Controls/UploadControl.php @@ -78,7 +78,7 @@ public function getHtmlName(): string * @return static * @internal */ - public function setValue($value) + public function setCurrentValue($value) { return $this; } diff --git a/src/Forms/IControl.php b/src/Forms/IControl.php index 891c7f127..8c4ef69fe 100644 --- a/src/Forms/IControl.php +++ b/src/Forms/IControl.php @@ -21,7 +21,7 @@ interface IControl * @param mixed * @return static */ - function setValue($value); + //function setCurrentValue($value); /** * Returns control's value. diff --git a/src/Forms/Rules.php b/src/Forms/Rules.php index 89c4aef6c..345fcbf8a 100644 --- a/src/Forms/Rules.php +++ b/src/Forms/Rules.php @@ -170,7 +170,7 @@ public function addFilter(callable $filter) $this->rules[] = $rule = new Rule; $rule->control = $this->control; $rule->validator = function (IControl $control) use ($filter) { - $control->setValue($filter($control->getValue())); + $control->setCurrentValue($filter($control->getValue())); return true; }; return $this; diff --git a/src/Forms/Validator.php b/src/Forms/Validator.php index bed08b42b..c76cd468e 100644 --- a/src/Forms/Validator.php +++ b/src/Forms/Validator.php @@ -236,7 +236,7 @@ public static function validateUrl(IControl $control): bool return true; } elseif (Validators::isUrl($value = "http://$value")) { - $control->setValue($value); + $control->setCurrentValue($value); return true; } return false; @@ -269,7 +269,7 @@ public static function validateInteger(IControl $control): bool { if (Validators::isNumericInt($value = $control->getValue())) { if (!is_float($tmp = $value * 1)) { // bigint leave as string - $control->setValue($tmp); + $control->setCurrentValue($tmp); } return true; } @@ -284,7 +284,7 @@ public static function validateFloat(IControl $control): bool { $value = str_replace([' ', ','], ['', '.'], $control->getValue()); if (Validators::isNumeric($value)) { - $control->setValue((float) $value); + $control->setCurrentValue((float) $value); return true; } return false; diff --git a/tests/Forms.Latte/FormMacros.forms.phpt b/tests/Forms.Latte/FormMacros.forms.phpt index 20f36fd8f..6f80f9f08 100644 --- a/tests/Forms.Latte/FormMacros.forms.phpt +++ b/tests/Forms.Latte/FormMacros.forms.phpt @@ -36,7 +36,7 @@ $form->addText('username', 'Username:'); // must have just one textfield to gene $form['username']->getControlPrototype()->addClass('control-class'); $form->addRadioList('sex', 'Sex:', ['m' => 'male', 'f' => 'female']); $form->addSelect('select', null, ['m' => 'male', 'f' => 'female']); -$form->addTextArea('area', null)->setValue('oneaddTextArea('area', null)->setDefaultValue('oneaddCheckbox('checkbox', 'Checkbox'); $form->addCheckboxList('checklist', 'CheckboxList:', ['m' => 'male', 'f' => 'female']); $form->addSubmit('send', 'Sign in'); diff --git a/tests/Forms/Container.validate().phpt b/tests/Forms/Container.validate().phpt index 9f9802652..ee37a5bec 100644 --- a/tests/Forms/Container.validate().phpt +++ b/tests/Forms/Container.validate().phpt @@ -26,7 +26,7 @@ $container->onValidate[] = function (Container $container) { $container['name']->addError('fail 2'); }; -$form->setValues(['name' => 'invalid*input']); +$form->setDefaults(['name' => 'invalid*input']); $form->validate(); Assert::same([ diff --git a/tests/Forms/Controls.BaseControl.phpt b/tests/Forms/Controls.BaseControl.phpt index e6b5b850b..1d9b7a332 100644 --- a/tests/Forms/Controls.BaseControl.phpt +++ b/tests/Forms/Controls.BaseControl.phpt @@ -37,7 +37,7 @@ test(function () { // error handling test(function () { // validators $form = new Form; $input = $form->addText('text'); - $input->setValue(123); + $input->setCurrentValue(123); Assert::true(Validator::validateEqual($input, 123)); Assert::true(Validator::validateEqual($input, '123')); @@ -74,7 +74,7 @@ test(function () { // validators test(function () { // validators for array $form = new Form; $input = $form->addMultiSelect('select', null, ['a', 'b', 'c', 'd']); - $input->setValue([1, 2, 3]); + $input->setCurrentValue([1, 2, 3]); Assert::true(Validator::validateEqual($input, [1, 2, 3, 4])); Assert::true(Validator::validateEqual($input, ['1', '2', '3'])); diff --git a/tests/Forms/Controls.Checkbox.loadData.phpt b/tests/Forms/Controls.Checkbox.loadData.phpt index 56a269476..4cd092267 100644 --- a/tests/Forms/Controls.Checkbox.loadData.phpt +++ b/tests/Forms/Controls.Checkbox.loadData.phpt @@ -49,12 +49,12 @@ test(function () { // malformed data }); -test(function () { // setValue() and invalid argument +test(function () { // setCurrentValue() and invalid argument $form = new Form; $input = $form->addCheckbox('checkbox'); - $input->setValue(null); + $input->setCurrentValue(null); Assert::exception(function () use ($input) { - $input->setValue([]); - }, Nette\InvalidArgumentException::class, "Value must be scalar or null, array given in field 'checkbox'."); + $input->setCurrentValue([]); + }, Nette\InvalidArgumentException::class, "Value must be scalar or NULL, array given in field 'checkbox'."); }); diff --git a/tests/Forms/Controls.Checkbox.render.phpt b/tests/Forms/Controls.Checkbox.render.phpt index 0209ddb5c..ef2cd1963 100644 --- a/tests/Forms/Controls.Checkbox.render.phpt +++ b/tests/Forms/Controls.Checkbox.render.phpt @@ -38,7 +38,7 @@ test(function () { Assert::type(Html::class, $input->getControlPart()); Assert::same('', (string) $input->getControlPart()); - $input->setValue(true); + $input->setCurrentValue(true); Assert::same('', (string) $input->getControl()); Assert::same('', (string) $input->getControlPart()); }); diff --git a/tests/Forms/Controls.CheckboxList.loadData.phpt b/tests/Forms/Controls.CheckboxList.loadData.phpt index 42d348345..f3aa7c945 100644 --- a/tests/Forms/Controls.CheckboxList.loadData.phpt +++ b/tests/Forms/Controls.CheckboxList.loadData.phpt @@ -131,13 +131,13 @@ test(function () use ($series) { // validateEqual }); -test(function () use ($series) { // setValue() and invalid argument +test(function () use ($series) { // setCurrentValue() and invalid argument $form = new Form; $input = $form->addCheckboxList('list', null, $series); - $input->setValue(null); + $input->setCurrentValue(null); Assert::exception(function () use ($input) { - $input->setValue('unknown'); + $input->setCurrentValue('unknown'); }, Nette\InvalidArgumentException::class, "Value 'unknown' are out of allowed set ['red-dwarf', 'the-simpsons', 0, ''] in field 'list'."); }); @@ -145,7 +145,7 @@ test(function () use ($series) { // setValue() and invalid argument test(function () { // object as value $form = new Form; $input = $form->addCheckboxList('list', null, ['2013-07-05 00:00:00' => 1]) - ->setValue([new DateTime('2013-07-05')]); + ->setCurrentValue([new DateTime('2013-07-05')]); Assert::same(['2013-07-05 00:00:00'], $input->getValue()); }); @@ -155,7 +155,7 @@ test(function () { // object as item $form = new Form; $input = $form->addCheckboxList('list') ->setItems([new DateTime('2013-07-05')], false) - ->setValue('2013-07-05 00:00:00'); + ->setCurrentValue('2013-07-05 00:00:00'); Assert::equal(['2013-07-05 00:00:00' => new DateTime('2013-07-05')], $input->getSelectedItems()); }); diff --git a/tests/Forms/Controls.CheckboxList.render.phpt b/tests/Forms/Controls.CheckboxList.render.phpt index 00f07bd59..19d7ec3aa 100644 --- a/tests/Forms/Controls.CheckboxList.render.phpt +++ b/tests/Forms/Controls.CheckboxList.render.phpt @@ -50,7 +50,7 @@ test(function () { // checked $input = $form->addCheckboxList('list', 'Label', [ 'a' => 'First', 0 => 'Second', - ])->setValue(0); + ])->setCurrentValue(0); Assert::same('
', (string) $input->getControl()); }); diff --git a/tests/Forms/Controls.ChoiceControl.loadData.phpt b/tests/Forms/Controls.ChoiceControl.loadData.phpt index 93925b01f..e9015b4eb 100644 --- a/tests/Forms/Controls.ChoiceControl.loadData.phpt +++ b/tests/Forms/Controls.ChoiceControl.loadData.phpt @@ -143,22 +143,22 @@ test(function () use ($series) { // setItems without keys }); -test(function () use ($series) { // setValue() and invalid argument +test(function () use ($series) { // setCurrentValue() and invalid argument $form = new Form; $input = $form['select'] = new ChoiceControl(null, $series); - $input->setValue(null); + $input->setCurrentValue(null); Assert::exception(function () use ($input) { - $input->setValue('unknown'); + $input->setCurrentValue('unknown'); }, Nette\InvalidArgumentException::class, "Value 'unknown' is out of allowed set ['red-dwarf', 'the-simpsons', 0, ''] in field 'select'."); }); -test(function () use ($series) { // setValue() and disabled checkDefaultValue() +test(function () use ($series) { // setCurrentValue() and disabled checkDefaultValue() $form = new Form; $input = $form['select'] = new ChoiceControl(null, $series); $input->checkDefaultValue(false); - $input->setValue('unknown'); + $input->setCurrentValue('unknown'); Assert::null($input->getValue()); }); @@ -166,7 +166,7 @@ test(function () use ($series) { // setValue() and disabled checkDefaultValue() test(function () { // object as value $form = new Form; $input = $form['select'] = new ChoiceControl(null, ['2013-07-05 00:00:00' => 1]); - $input->setValue(new DateTime('2013-07-05')); + $input->setCurrentValue(new DateTime('2013-07-05')); Assert::same('2013-07-05 00:00:00', $input->getValue()); }); @@ -176,7 +176,7 @@ test(function () { // object as item $form = new Form; $input = $form['select'] = new ChoiceControl; $input->setItems([new DateTime('2013-07-05')], false) - ->setValue(new DateTime('2013-07-05')); + ->setCurrentValue(new DateTime('2013-07-05')); Assert::same('2013-07-05 00:00:00', $input->getValue()); }); diff --git a/tests/Forms/Controls.CsrfProtection.breachAttack.phpt b/tests/Forms/Controls.CsrfProtection.breachAttack.phpt index 52ad9f6a8..64911c646 100644 --- a/tests/Forms/Controls.CsrfProtection.breachAttack.phpt +++ b/tests/Forms/Controls.CsrfProtection.breachAttack.phpt @@ -16,9 +16,9 @@ require __DIR__ . '/../bootstrap.php'; $form = new Form; $input = $form->addProtection('Security token did not match. Possible CSRF attack.'); -$token = $input->getControl()->value; +$token = $input->getControl()->getValue(); -Assert::notSame($token, $input->getControl()->value); +Assert::notSame($token, $input->getControl()->getValue()); /* BREACH attack test: skipped because $input->getControl() always generates different token $charlist = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; @@ -52,7 +52,7 @@ for ($i = 3; $i <= strlen($token); $i++) { } foreach ($strings as $string) { - $input->setValue($string); + $input->setCurrentValue($string); Assert::false(CsrfProtection::validateCsrf($input)); } */ diff --git a/tests/Forms/Controls.CsrfProtection.phpt b/tests/Forms/Controls.CsrfProtection.phpt index 2370ae987..afee5d43b 100644 --- a/tests/Forms/Controls.CsrfProtection.phpt +++ b/tests/Forms/Controls.CsrfProtection.phpt @@ -29,18 +29,18 @@ Assert::match('', (string) $inpu Assert::true($input->getOption('rendered')); Assert::same('hidden', $input->getOption('type')); -$input->setValue(null); +$input->setCurrentValue(null); Assert::false(CsrfProtection::validateCsrf($input)); -call_user_func([$input, 'Nette\Forms\Controls\BaseControl::setValue'], '12345678901234567890123456789012345678'); +call_user_func([$input, 'Nette\Forms\Controls\BaseControl::setCurrentValue'], '12345678901234567890123456789012345678'); Assert::false(CsrfProtection::validateCsrf($input)); -$value = $input->getControl()->value; -call_user_func([$input, 'Nette\Forms\Controls\BaseControl::setValue'], $value); +$value = $input->getControl()->getValue(); +call_user_func([$input, 'Nette\Forms\Controls\BaseControl::setCurrentValue'], $value); Assert::true(CsrfProtection::validateCsrf($input)); session_regenerate_id(); -call_user_func([$input, 'Nette\Forms\Controls\BaseControl::setValue'], $value); +call_user_func([$input, 'Nette\Forms\Controls\BaseControl::setCurrentValue'], $value); Assert::false(CsrfProtection::validateCsrf($input)); diff --git a/tests/Forms/Controls.HiddenField.loadData.phpt b/tests/Forms/Controls.HiddenField.loadData.phpt index ee9633938..a66529bd4 100644 --- a/tests/Forms/Controls.HiddenField.loadData.phpt +++ b/tests/Forms/Controls.HiddenField.loadData.phpt @@ -54,21 +54,21 @@ test(function () { // errors are moved to form }); -test(function () { // setValue() and invalid argument +test(function () { // setCurrentValue() and invalid argument $form = new Form; $input = $form->addHidden('hidden'); - $input->setValue(null); + $input->setCurrentValue(null); Assert::exception(function () use ($input) { - $input->setValue([]); - }, Nette\InvalidArgumentException::class, "Value must be scalar or null, array given in field 'hidden'."); + $input->setCurrentValue([]); + }, Nette\InvalidArgumentException::class, "Value must be scalar or NULL, array given in field 'hidden'."); }); test(function () { // object $form = new Form; $input = $form->addHidden('hidden') - ->setValue(new Nette\Utils\DateTime('2013-07-05')); + ->setCurrentValue(new Nette\Utils\DateTime('2013-07-05')); Assert::same('2013-07-05 00:00:00', $input->getValue()); }); @@ -77,7 +77,7 @@ test(function () { // object test(function () { // persistent $form = new Form; $input = $form['hidden'] = new Nette\Forms\Controls\HiddenField('persistent'); - $input->setValue('other'); + $input->setCurrentValue('other'); Assert::same('persistent', $input->getValue()); }); diff --git a/tests/Forms/Controls.MultiChoiceControl.loadData.phpt b/tests/Forms/Controls.MultiChoiceControl.loadData.phpt index 8c3a24046..8cbbb3b23 100644 --- a/tests/Forms/Controls.MultiChoiceControl.loadData.phpt +++ b/tests/Forms/Controls.MultiChoiceControl.loadData.phpt @@ -156,38 +156,39 @@ test(function () use ($series) { // validateEqual }); -test(function () use ($series) { // setValue() and invalid argument +test(function () use ($series) { // setCurrentValue() and invalid argument $form = new Form; $input = $form['select'] = new MultiChoiceControl(null, $series); - $input->setValue(null); + $input->setCurrentValue(null); Assert::exception(function () use ($input) { - $input->setValue('unknown'); + $input->setCurrentValue('unknown'); }, Nette\InvalidArgumentException::class, "Value 'unknown' are out of allowed set ['red-dwarf', 'the-simpsons', 0, ''] in field 'select'."); Assert::exception(function () use ($input) { - $input->setValue(new stdClass); - }, Nette\InvalidArgumentException::class, "Value must be array or null, object given in field 'select'."); + $input->setCurrentValue(new stdClass); + }, Nette\InvalidArgumentException::class, "Value must be array or NULL, object given in field 'select'."); Assert::exception(function () use ($input) { - $input->setValue([new stdClass]); + $input->setCurrentValue([new stdClass]); }, Nette\InvalidArgumentException::class, "Values must be scalar, object given in field 'select'."); }); -test(function () use ($series) { // setValue() and disabled checkDefaultValue() +test(function () use ($series) { // setCurrentValue() and disabled checkDefaultValue() $form = new Form; + $input->setCurrentValue('unknown'); $input = $form['select'] = new MultiChoiceControl(null, $series); $input->checkDefaultValue(false); - $input->setValue('unknown'); + $input->setCurrentValue('unknown'); Assert::same([], $input->getValue()); Assert::exception(function () use ($input) { - $input->setValue(new stdClass); - }, Nette\InvalidArgumentException::class, "Value must be array or null, object given in field 'select'."); + $input->setCurrentValue(new stdClass); + }, Nette\InvalidArgumentException::class, "Value must be array or NULL, object given in field 'select'."); Assert::exception(function () use ($input) { - $input->setValue([new stdClass]); + $input->setCurrentValue([new stdClass]); }, Nette\InvalidArgumentException::class, "Values must be scalar, object given in field 'select'."); }); @@ -195,7 +196,7 @@ test(function () use ($series) { // setValue() and disabled checkDefaultValue() test(function () { // object as value $form = new Form; $input = $form['select'] = new MultiChoiceControl(null, ['2013-07-05 00:00:00' => 1]); - $input->setValue([new DateTime('2013-07-05')]); + $input->setCurrentValue([new DateTime('2013-07-05')]); Assert::same(['2013-07-05 00:00:00'], $input->getValue()); }); diff --git a/tests/Forms/Controls.MultiSelectBox.loadData.phpt b/tests/Forms/Controls.MultiSelectBox.loadData.phpt index 4faea1ab7..23ce1291e 100644 --- a/tests/Forms/Controls.MultiSelectBox.loadData.phpt +++ b/tests/Forms/Controls.MultiSelectBox.loadData.phpt @@ -194,13 +194,13 @@ test(function () { // setItems without keys with optgroups }); -test(function () use ($series) { // setValue() and invalid argument +test(function () use ($series) { // setCurrentValue() and invalid argument $form = new Form; $input = $form->addMultiSelect('select', null, $series); - $input->setValue(null); + $input->setCurrentValue(null); Assert::exception(function () use ($input) { - $input->setValue('unknown'); + $input->setCurrentValue('unknown'); }, Nette\InvalidArgumentException::class, "Value 'unknown' are out of allowed set ['red-dwarf', 'the-simpsons', 0, ''] in field 'select'."); }); @@ -208,7 +208,7 @@ test(function () use ($series) { // setValue() and invalid argument test(function () { // object as value $form = new Form; $input = $form->addMultiSelect('select', null, ['2013-07-05 00:00:00' => 1]) - ->setValue([new DateTime('2013-07-05')]); + ->setCurrentValue([new DateTime('2013-07-05')]); Assert::same(['2013-07-05 00:00:00'], $input->getValue()); }); @@ -221,7 +221,7 @@ test(function () { // object as item 'group' => [new DateTime('2013-07-05')], new DateTime('2013-07-06'), ], false) - ->setValue('2013-07-05 00:00:00'); + ->setCurrentValue('2013-07-05 00:00:00'); Assert::equal(['2013-07-05 00:00:00' => new DateTime('2013-07-05')], $input->getSelectedItems()); }); diff --git a/tests/Forms/Controls.MultiSelectBox.render.phpt b/tests/Forms/Controls.MultiSelectBox.render.phpt index e67a2d75d..241b23fbc 100644 --- a/tests/Forms/Controls.MultiSelectBox.render.phpt +++ b/tests/Forms/Controls.MultiSelectBox.render.phpt @@ -44,7 +44,7 @@ test(function () { // selected $input = $form->addMultiSelect('list', 'Label', [ 'a' => 'First', 0 => 'Second', - ])->setValue(0); + ])->setCurrentValue(0); Assert::same('', (string) $input->getControl()); }); @@ -151,7 +151,7 @@ test(function () { $input = $form->addMultiSelect('list', 'Label', [ 1 => 'First', 2 => 'Second', - ])->setValue(1); + ])->setCurrentValue(1); $input->addOptionAttributes(['bar' => 'b', 'selected?' => 2, 'foo:' => [1 => 'a', 2 => 'b']]); $input->addOptionAttributes(['bar' => 'c']); Assert::same('', (string) $input->getControl()); diff --git a/tests/Forms/Controls.RadioList.loadData.phpt b/tests/Forms/Controls.RadioList.loadData.phpt index 64a30990c..a8422c0f3 100644 --- a/tests/Forms/Controls.RadioList.loadData.phpt +++ b/tests/Forms/Controls.RadioList.loadData.phpt @@ -131,13 +131,13 @@ test(function () use ($series) { // setItems without keys }); -test(function () use ($series) { // setValue() and invalid argument +test(function () use ($series) { // setCurrentValue() and invalid argument $form = new Form; $input = $form->addRadioList('radio', null, $series); - $input->setValue(null); + $input->setCurrentValue(null); Assert::exception(function () use ($input) { - $input->setValue('unknown'); + $input->setCurrentValue('unknown'); }, Nette\InvalidArgumentException::class, "Value 'unknown' is out of allowed set ['red-dwarf', 'the-simpsons', 0, ''] in field 'radio'."); }); @@ -145,7 +145,7 @@ test(function () use ($series) { // setValue() and invalid argument test(function () { // object as value $form = new Form; $input = $form->addRadioList('radio', null, ['2013-07-05 00:00:00' => 1]) - ->setValue(new DateTime('2013-07-05')); + ->setCurrentValue(new DateTime('2013-07-05')); Assert::same('2013-07-05 00:00:00', $input->getValue()); }); @@ -155,7 +155,7 @@ test(function () { // object as item $form = new Form; $input = $form->addRadioList('radio') ->setItems([new DateTime('2013-07-05')], false) - ->setValue(new DateTime('2013-07-05')); + ->setCurrentValue(new DateTime('2013-07-05')); Assert::same('2013-07-05 00:00:00', $input->getValue()); }); diff --git a/tests/Forms/Controls.RadioList.render.phpt b/tests/Forms/Controls.RadioList.render.phpt index f691789b4..a4d5f1a57 100644 --- a/tests/Forms/Controls.RadioList.render.phpt +++ b/tests/Forms/Controls.RadioList.render.phpt @@ -50,7 +50,7 @@ test(function () { // checked $input = $form->addRadioList('list', 'Label', [ 'a' => 'First', 0 => 'Second', - ])->setValue(0); + ])->setCurrentValue(0); Assert::same('
', (string) $input->getControl()); }); diff --git a/tests/Forms/Controls.SelectBox.isOk.phpt b/tests/Forms/Controls.SelectBox.isOk.phpt index 094c69368..965a8cdae 100644 --- a/tests/Forms/Controls.SelectBox.isOk.phpt +++ b/tests/Forms/Controls.SelectBox.isOk.phpt @@ -27,9 +27,9 @@ $select->setPrompt('Empty'); Assert::true($select->isOk()); $select->setPrompt(false); -$select->setValue('bar'); +$select->setCurrentValue('bar'); Assert::true($select->isOk()); -$select->setValue(null); +$select->setCurrentValue(null); $select->setItems([]); Assert::true($select->isOk()); diff --git a/tests/Forms/Controls.SelectBox.loadData.phpt b/tests/Forms/Controls.SelectBox.loadData.phpt index d99c198fc..d9de1ddf6 100644 --- a/tests/Forms/Controls.SelectBox.loadData.phpt +++ b/tests/Forms/Controls.SelectBox.loadData.phpt @@ -229,13 +229,13 @@ test(function () { // setItems without keys with optgroups }); -test(function () use ($series) { // setValue() and invalid argument +test(function () use ($series) { // setCurrentValue() and invalid argument $form = new Form; $input = $form->addSelect('select', null, $series); - $input->setValue(null); + $input->setCurrentValue(null); Assert::exception(function () use ($input) { - $input->setValue('unknown'); + $input->setCurrentValue('unknown'); }, Nette\InvalidArgumentException::class, "Value 'unknown' is out of allowed set ['red-dwarf', 'the-simpsons', 0, ''] in field 'select'."); }); @@ -243,7 +243,7 @@ test(function () use ($series) { // setValue() and invalid argument test(function () { // object as value $form = new Form; $input = $form->addSelect('select', null, ['2013-07-05 00:00:00' => 1]) - ->setValue(new DateTime('2013-07-05')); + ->setCurrentValue(new DateTime('2013-07-05')); Assert::same('2013-07-05 00:00:00', $input->getValue()); }); @@ -256,7 +256,7 @@ test(function () { // object as item 'group' => [new DateTime('2013-07-05')], new DateTime('2013-07-06'), ], false) - ->setValue('2013-07-05 00:00:00'); + ->setCurrentValue('2013-07-05 00:00:00'); Assert::equal(new DateTime('2013-07-05'), $input->getSelectedItem()); }); diff --git a/tests/Forms/Controls.SelectBox.render.phpt b/tests/Forms/Controls.SelectBox.render.phpt index 6c8a9e11e..e512c61a7 100644 --- a/tests/Forms/Controls.SelectBox.render.phpt +++ b/tests/Forms/Controls.SelectBox.render.phpt @@ -44,7 +44,7 @@ test(function () { // selected $input = $form->addSelect('list', 'Label', [ 'a' => 'First', 0 => 'Second', - ])->setValue(0); + ])->setCurrentValue(0); Assert::same('', (string) $input->getControl()); }); @@ -151,7 +151,7 @@ test(function () { $input = $form->addSelect('list', 'Label', [ 1 => 'First', 2 => 'Second', - ])->setValue(1); + ])->setCurrentValue(1); $input->addOptionAttributes(['bar' => 'b', 'selected?' => 2, 'foo:' => [1 => 'a', 2 => 'b']]); $input->addOptionAttributes(['bar' => 'c']); Assert::same('', (string) $input->getControl()); diff --git a/tests/Forms/Controls.TestBase.validators.phpt b/tests/Forms/Controls.TestBase.validators.phpt index 29d3de63b..073ae8cb6 100644 --- a/tests/Forms/Controls.TestBase.validators.phpt +++ b/tests/Forms/Controls.TestBase.validators.phpt @@ -18,7 +18,7 @@ require __DIR__ . '/../bootstrap.php'; test(function () { $control = new TextInput; - $control->value = ''; + $control->setCurrentValue(''); Assert::true(Validator::validateMinLength($control, 0)); Assert::false(Validator::validateMinLength($control, 1)); }); @@ -26,10 +26,10 @@ test(function () { test(function () { $control = new TextInput; - $control->value = ''; + $control->setCurrentValue(''); Assert::true(Validator::validateMaxLength($control, 0)); - $control->value = 'aaa'; + $control->setCurrentValue('aaa'); Assert::false(Validator::validateMaxLength($control, 2)); Assert::true(Validator::validateMaxLength($control, 3)); }); @@ -37,11 +37,11 @@ test(function () { test(function () { $control = new TextInput; - $control->value = ''; + $control->setCurrentValue(''); Assert::true(Validator::validateLength($control, 0)); Assert::true(Validator::validateLength($control, [0, 0])); - $control->value = 'aaa'; + $control->setCurrentValue('aaa'); Assert::true(Validator::validateLength($control, 3)); Assert::false(Validator::validateLength($control, 4)); Assert::true(Validator::validateLength($control, [3])); @@ -51,45 +51,45 @@ test(function () { test(function () { $control = new TextInput; - $control->value = ''; + $control->setCurrentValue(''); Assert::false(Validator::validateEmail($control)); - $control->value = '@.'; + $control->setCurrentValue('@.'); Assert::false(Validator::validateEmail($control)); - $control->value = 'name@a-b-c.cz'; + $control->setCurrentValue('name@a-b-c.cz'); Assert::true(Validator::validateEmail($control)); - $control->value = "name@\u{17E}lu\u{165}ou\u{10D}k\u{FD}.cz"; // name@žluťoučký.cz + $control->setCurrentValue("name@\u{17E}lu\u{165}ou\u{10D}k\u{FD}.cz"); // name@žluťoučký.cz Assert::true(Validator::validateEmail($control)); - $control->value = "\u{17E}name@\u{17E}lu\u{165}ou\u{10D}k\u{FD}.cz"; // žname@žluťoučký.cz + $control->setCurrentValue("\u{17E}name@\u{17E}lu\u{165}ou\u{10D}k\u{FD}.cz"); // žname@žluťoučký.cz Assert::false(Validator::validateEmail($control)); }); test(function () { $control = new TextInput; - $control->value = ''; + $control->setCurrentValue(''); Assert::false(Validator::validateUrl($control)); Assert::same('', $control->value); - $control->value = 'localhost'; + $control->setCurrentValue('localhost'); Assert::true(Validator::validateUrl($control)); Assert::same('http://localhost', $control->value); - $control->value = 'http://nette.org'; + $control->setCurrentValue('http://nette.org'); Assert::true(Validator::validateUrl($control)); Assert::same('http://nette.org', $control->value); - $control->value = '/nette.org'; + $control->setCurrentValue('/nette.org'); Assert::false(Validator::validateUrl($control)); }); test(function () { $control = new TextInput; - $control->value = '123x'; + $control->setCurrentValue('123x'); Assert::false(Validator::validatePattern($control, '[0-9]')); Assert::true(Validator::validatePattern($control, '[0-9]+x')); Assert::false(Validator::validatePattern($control, '[0-9]+X')); @@ -117,7 +117,7 @@ test(function () { test(function () { $control = new TextInput; - $control->value = ''; + $control->setCurrentValue(''); Assert::false(Validator::validateNumeric($control)); Assert::same('', $control->value); @@ -143,19 +143,19 @@ test(function () { Assert::false(Validator::validateInteger($control)); Assert::same('', $control->value); - $control->value = '-123'; + $control->setCurrentValue('-123'); Assert::true(Validator::validateInteger($control)); Assert::same(-123, $control->value); - $control->value = '123,5'; + $control->setCurrentValue('123,5'); Assert::false(Validator::validateInteger($control)); Assert::same('123,5', $control->value); - $control->value = '123.5'; + $control->setCurrentValue('123.5'); Assert::false(Validator::validateInteger($control)); Assert::same('123.5', $control->value); - $control->value = PHP_INT_MAX . PHP_INT_MAX; + $control->setCurrentValue(PHP_INT_MAX . PHP_INT_MAX); Assert::true(Validator::validateInteger($control)); Assert::same(PHP_INT_MAX . PHP_INT_MAX, $control->value); }); @@ -163,23 +163,23 @@ test(function () { test(function () { $control = new TextInput; - $control->value = ''; + $control->setCurrentValue(''); Assert::false(Validator::validateFloat($control)); Assert::same('', $control->value); - $control->value = '-123'; + $control->setCurrentValue('-123'); Assert::true(Validator::validateFloat($control)); Assert::same(-123.0, $control->value); - $control->value = '123,5'; + $control->setCurrentValue('123,5'); Assert::true(Validator::validateFloat($control)); Assert::same(123.5, $control->value); - $control->value = '123.5'; + $control->setCurrentValue('123.5'); Assert::true(Validator::validateFloat($control)); Assert::same(123.5, $control->value); - $control->value = PHP_INT_MAX . PHP_INT_MAX; + $control->setCurrentValue(PHP_INT_MAX . PHP_INT_MAX); Assert::true(Validator::validateFloat($control)); Assert::same((float) (PHP_INT_MAX . PHP_INT_MAX), $control->value); }); diff --git a/tests/Forms/Controls.TextArea.render.phpt b/tests/Forms/Controls.TextArea.render.phpt index 9f27b6f2a..c932640bd 100644 --- a/tests/Forms/Controls.TextArea.render.phpt +++ b/tests/Forms/Controls.TextArea.render.phpt @@ -26,7 +26,7 @@ class Translator implements Nette\Localization\ITranslator test(function () { $form = new Form; $input = $form->addTextArea('text', 'Label') - ->setValue('&text') + ->setCurrentValue('&text') ->setHtmlAttribute('autocomplete', 'off'); Assert::type(Html::class, $input->getLabel()); @@ -42,7 +42,7 @@ test(function () { // translator $form = new Form; $input = $form->addTextArea('text', 'Label') ->setHtmlAttribute('placeholder', 'place') - ->setValue('text') + ->setCurrentValue('text') ->setTranslator(new Translator); Assert::same('', (string) $input->getLabel()); diff --git a/tests/Forms/Controls.TextBase.loadData.phpt b/tests/Forms/Controls.TextBase.loadData.phpt index 5a0362f5c..9a69688c3 100644 --- a/tests/Forms/Controls.TextBase.loadData.phpt +++ b/tests/Forms/Controls.TextBase.loadData.phpt @@ -91,16 +91,16 @@ test(function () { // malformed data }); -test(function () { // setValue() and invalid argument +test(function () { // setCurrentValue() and invalid argument $_POST = ['text' => " a\r b \n c "]; $form = new Form; $input = $form->addText('text'); - $input->setValue(null); + $input->setCurrentValue(null); Assert::exception(function () use ($input) { - $input->setValue([]); - }, Nette\InvalidArgumentException::class, "Value must be scalar or null, array given in field 'text'."); + $input->setCurrentValue([]); + }, Nette\InvalidArgumentException::class, "Value must be scalar or NULL, array given in field 'text'."); }); @@ -158,7 +158,7 @@ test(function () { // URL test(function () { // object $form = new Form; $input = $form->addText('text') - ->setValue($date = new Nette\Utils\DateTime('2013-07-05')); + ->setCurrentValue($date = new Nette\Utils\DateTime('2013-07-05')); Assert::same($date, $input->getValue()); }); diff --git a/tests/Forms/Controls.TextInput.render.phpt b/tests/Forms/Controls.TextInput.render.phpt index 01f4b81f8..fcf9f28e0 100644 --- a/tests/Forms/Controls.TextInput.render.phpt +++ b/tests/Forms/Controls.TextInput.render.phpt @@ -26,7 +26,7 @@ class Translator implements Nette\Localization\ITranslator test(function () { $form = new Form; $input = $form->addText('text', 'Label') - ->setValue('text') + ->setCurrentValue('text') ->setHtmlAttribute('autocomplete', 'off'); Assert::type(Html::class, $input->getLabel()); @@ -42,7 +42,7 @@ test(function () { // translator $form = new Form; $input = $form->addText('text', 'Label') ->setHtmlAttribute('placeholder', 'place') - ->setValue('text') + ->setCurrentValue('text') ->setTranslator(new Translator) ->setEmptyValue('xxx'); @@ -65,7 +65,7 @@ test(function () { // Html with translator test(function () { // password $form = new Form; $input = $form->addPassword('password') - ->setValue('xxx'); + ->setCurrentValue('xxx'); Assert::same('', (string) $input->getControl()); }); diff --git a/tests/Forms/Forms.userValidator.phpt b/tests/Forms/Forms.userValidator.phpt index 46069de3a..f8a988dc9 100644 --- a/tests/Forms/Forms.userValidator.phpt +++ b/tests/Forms/Forms.userValidator.phpt @@ -32,6 +32,6 @@ foreach ($datasets as $case) { ->addRule('myValidator1', 'Value %d is not allowed!', 11) ->addRule(~'myValidator1', 'Value %d is required!', 22); // @ - negative rules are deprecated - $control->setValue($case[0])->validate(); + $control->setDefaultValue($case[0])->validate(); Assert::same($case[1], $control->getErrors()); } diff --git a/tests/Forms/Rules.dynamic.phpt b/tests/Forms/Rules.dynamic.phpt index 7eaf1a7ed..4247f9aa7 100644 --- a/tests/Forms/Rules.dynamic.phpt +++ b/tests/Forms/Rules.dynamic.phpt @@ -26,7 +26,7 @@ foreach ($datasets as $case) { $form->addText('min'); $form->addText('max'); $form->addText('value')->addRule(Form::RANGE, null, [$form['min'], $form['max']]); - $form->setValues($case[0]); + $form->setCurrentValues($case[0]); Assert::equal($case[1], $form->isValid()); } diff --git a/tests/Forms/Rules.valid.phpt b/tests/Forms/Rules.valid.phpt index d695b1678..b29bd44b5 100644 --- a/tests/Forms/Rules.valid.phpt +++ b/tests/Forms/Rules.valid.phpt @@ -24,11 +24,11 @@ test(function () { $form->validate(); Assert::same(['fill foo'], $form->getErrors()); - $form['foo']->setValue('abc'); + $form['foo']->setDefaultValue('abc'); $form->validate(); Assert::same(['fill bar'], $form->getErrors()); - $form['bar']->setValue('abc'); + $form['bar']->setDefaultValue('abc'); $form->validate(); Assert::same([], $form->getErrors()); });