Skip to content

IControl::setValue() ⇒ setCurrentValues() #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/custom-control.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down
28 changes: 19 additions & 9 deletions src/Forms/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -68,25 +68,35 @@ 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);
}
}
}
return $this;
}


/**
* @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
Expand Down
22 changes: 15 additions & 7 deletions src/Forms/Controls/BaseControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -92,7 +92,7 @@ public function __construct($caption = null)
if (self::$autoOptional) {
$this->setRequired(false);
}
$this->setValue(null);
$this->setCurrentValue(null);
}


Expand Down Expand Up @@ -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));
}


Expand Down Expand Up @@ -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;
}


Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Controls/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Controls/ChoiceControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '...');
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Controls/CsrfProtection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Controls/HiddenField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Controls/MultiChoiceControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Controls/TextBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract class TextBase extends BaseControl
* @return static
* @internal
*/
public function setValue($value)
public function setCurrentValue($value)
{
if ($value === null) {
$value = '';
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Controls/TextInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}


Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Controls/UploadControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function getHtmlName(): string
* @return static
* @internal
*/
public function setValue($value)
public function setCurrentValue($value)
{
return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/IControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface IControl
* @param mixed
* @return static
*/
function setValue($value);
//function setCurrentValue($value);

/**
* Returns control's value.
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Forms/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/Forms.Latte/FormMacros.forms.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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('one<two');
$form->addTextArea('area', null)->setDefaultValue('one<two');
$form->addCheckbox('checkbox', 'Checkbox');
$form->addCheckboxList('checklist', 'CheckboxList:', ['m' => 'male', 'f' => 'female']);
$form->addSubmit('send', 'Sign in');
Expand Down
2 changes: 1 addition & 1 deletion tests/Forms/Container.validate().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
4 changes: 2 additions & 2 deletions tests/Forms/Controls.BaseControl.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down Expand Up @@ -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']));
Expand Down
8 changes: 4 additions & 4 deletions tests/Forms/Controls.Checkbox.loadData.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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'.");
});
2 changes: 1 addition & 1 deletion tests/Forms/Controls.Checkbox.render.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test(function () {
Assert::type(Html::class, $input->getControlPart());
Assert::same('<input type="checkbox" name="on" id="frm-on">', (string) $input->getControlPart());

$input->setValue(true);
$input->setCurrentValue(true);
Assert::same('<label for="frm-on"><input type="checkbox" name="on" id="frm-on" checked>Label</label>', (string) $input->getControl());
Assert::same('<input type="checkbox" name="on" id="frm-on" checked>', (string) $input->getControlPart());
});
Expand Down
10 changes: 5 additions & 5 deletions tests/Forms/Controls.CheckboxList.loadData.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,21 @@ 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'.");
});


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());
});
Expand All @@ -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());
});
Expand Down
2 changes: 1 addition & 1 deletion tests/Forms/Controls.CheckboxList.render.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test(function () { // checked
$input = $form->addCheckboxList('list', 'Label', [
'a' => 'First',
0 => 'Second',
])->setValue(0);
])->setCurrentValue(0);

Assert::same('<label><input type="checkbox" name="list[]" value="a">First</label><br><label><input type="checkbox" name="list[]" checked value="0">Second</label>', (string) $input->getControl());
});
Expand Down
Loading