Permalink
Browse files

Container::setValues() & setDefaults() accepts plain objects

  • Loading branch information...
dg committed Feb 6, 2019
1 parent 73720c7 commit 032cf954ded221a173387901217a64b11f5009a3
Showing with 256 additions and 7 deletions.
  1. +12 −7 src/Forms/Container.php
  2. +244 −0 tests/Forms/Container.values.mapping.phpt
@@ -41,30 +41,35 @@ class Container extends Nette\ComponentModel\Container implements \ArrayAccess
/**
* Fill-in with default values.
* @param array|object $data
* @return static
*/
public function setDefaults(iterable $values, bool $erase = false)
public function setDefaults($data, bool $erase = false)
{
$form = $this->getForm(false);
if (!$form || !$form->isAnchored() || !$form->isSubmitted()) {
$this->setValues($values, $erase);
$this->setValues($data, $erase);
}
return $this;
}
/**
* Fill-in with values.
* @param array|object $data
* @return static
* @internal
*/
public function setValues(iterable $values, bool $erase = false)
public function setValues($data, bool $erase = false)
{
if ($values instanceof \Traversable) {
$values = iterator_to_array($values);
if ($data instanceof \Traversable) {
$values = iterator_to_array($data);
} elseif (!is_array($values)) {
throw new Nette\InvalidArgumentException(sprintf('First parameter must be an array, %s given.', gettype($values)));
} elseif (is_object($data) || is_array($data) || $data === null) {
$values = (array) $data;
} else {
throw new Nette\InvalidArgumentException(sprintf('First parameter must be an array or object, %s given.', gettype($data)));
}
foreach ($this->getComponents() as $name => $control) {
@@ -0,0 +1,244 @@
<?php
declare(strict_types=1);
use Nette\Forms\Form;
use Nette\Utils\ArrayHash;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
class FormData
{
/** @var string */
public $title;
/** @var FormFirstLevel */
public $first;
}
class FormFirstLevel
{
/** @var string */
public $name;
/** @var int */
public $age;
/** @var FormSecondLevel */
public $second;
}
class FormSecondLevel
{
/** @var string */
public $city;
}
function hydrate(string $class, array $data)
{
$obj = new $class;
foreach ($data as $key => $value) {
$obj->$key = $value;
}
return $obj;
}
$_POST = [
'title' => 'sent title',
'first' => [
'age' => '999',
'second' => [
'city' => 'sent city',
],
],
];
function createForm(): Form
{
$form = new Form;
$form->addText('title');
$first = $form->addContainer('first');
$first->addText('name');
$first->addInteger('age');
$second = $first->addContainer('second');
$second->addText('city');
return $form;
}
test(function () { // setDefaults() + object
$form = createForm();
Assert::false($form->isSubmitted());
$form->setDefaults(hydrate(FormData::class, [
'title' => 'xxx',
'extra' => '50',
'first' => hydrate(FormFirstLevel::class, [
'name' => 'yyy',
'age' => '30',
'second' => hydrate(FormSecondLevel::class, [
'city' => 'zzz',
]),
]),
]));
Assert::same([
'title' => 'xxx',
'first' => [
'name' => 'yyy',
'age' => '30',
'second' => [
'city' => 'zzz',
],
],
], $form->getValues(true));
});
test(function () { // submitted form + getValues()
$_SERVER['REQUEST_METHOD'] = 'POST';
$form = createForm();
Assert::truthy($form->isSubmitted());
Assert::equal(ArrayHash::from([
'title' => 'sent title',
'first' => ArrayHash::from([
'name' => '',
'age' => '999',
'second' => ArrayHash::from([
'city' => 'sent city',
]),
]),
]), $form->getValues());
});
test(function () { // submitted form + reset()
$_SERVER['REQUEST_METHOD'] = 'POST';
$form = createForm();
Assert::truthy($form->isSubmitted());
$form->reset();
Assert::false($form->isSubmitted());
Assert::equal(ArrayHash::from([
'title' => '',
'first' => ArrayHash::from([
'name' => '',
'age' => null,
'second' => ArrayHash::from([
'city' => '',
]),
]),
]), $form->getValues());
});
test(function () { // setValues() + object
$_SERVER['REQUEST_METHOD'] = 'POST';
$form = createForm();
Assert::truthy($form->isSubmitted());
$form->setValues(hydrate(FormData::class, [
'title' => 'new1',
'first' => hydrate(FormFirstLevel::class, [
'name' => 'new2',
// age => null
]),
]));
Assert::equal(ArrayHash::from([
'title' => 'new1',
'first' => ArrayHash::from([
'name' => 'new2',
'age' => null,
'second' => ArrayHash::from([
'city' => 'sent city',
]),
]),
]), $form->getValues());
// erase
$form->setValues(hydrate(FormData::class, [
'title' => 'new1',
'first' => hydrate(FormFirstLevel::class, [
'name' => 'new2',
]),
]), true);
Assert::equal(ArrayHash::from([
'title' => 'new1',
'first' => ArrayHash::from([
'name' => 'new2',
'age' => null,
'second' => ArrayHash::from([
'city' => '',
]),
]),
]), $form->getValues());
});
test(function () { // onSuccess test
$_SERVER['REQUEST_METHOD'] = 'POST';
$form = createForm();
$form->onSuccess[] = function (Form $form, array $values) {
Assert::same([
'title' => 'sent title',
'first' => [
'name' => '',
'age' => 999,
'second' => [
'city' => 'sent city',
],
],
], $values);
};
$form->onSuccess[] = function (Form $form, ArrayHash $values) {
Assert::equal(ArrayHash::from([
'title' => 'sent title',
'first' => ArrayHash::from([
'name' => '',
'age' => 999,
'second' => ArrayHash::from([
'city' => 'sent city',
]),
]),
]), $values);
};
$form->onSuccess[] = function (Form $form, $values) {
Assert::equal(ArrayHash::from([
'title' => 'sent title',
'first' => ArrayHash::from([
'name' => '',
'age' => 999,
'second' => ArrayHash::from([
'city' => 'sent city',
]),
]),
]), $values);
};
$ok = false;
$form->onSuccess[] = function () use (&$ok) {
$ok = true;
};
$form->fireEvents();
Assert::true($ok);
});

0 comments on commit 032cf95

Please sign in to comment.