Skip to content

Commit

Permalink
added Form::reset()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 18, 2017
1 parent 7b4b525 commit d56f78c
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,18 @@ private function invokeHandlers($handlers, $button = null)
}


/**
* Resets form.
* @return static
*/
public function reset()
{
$this->setSubmittedBy(null);
$this->setValues([], true);
return $this;
}


/**
* Internal: returns submitted HTTP data or null when form was not submitted.
* @return array|null
Expand Down
83 changes: 83 additions & 0 deletions tests/Forms/Container.values.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,86 @@ Assert::equal(ArrayHash::from([
'name' => '',
]),
]), $form->getValues());


$form->setValues([
'name' => 'new1',
'first' => [
'name' => 'new2',
],
]);

Assert::truthy($form->isSubmitted());
Assert::equal(ArrayHash::from([
'name' => 'new1',
'first' => ArrayHash::from([
'name' => 'new2',
'age' => '40',
'second' => ArrayHash::from([
'name' => 'david',
]),
]),
'invalid' => ArrayHash::from([
'name' => '',
]),
]), $form->getValues());


$form->setValues([
'name' => 'new1',
'first' => [
'name' => 'new2',
],
], true);

Assert::truthy($form->isSubmitted());
Assert::equal(ArrayHash::from([
'name' => 'new1',
'first' => ArrayHash::from([
'name' => 'new2',
'age' => '',
'second' => ArrayHash::from([
'name' => '',
]),
]),
'invalid' => ArrayHash::from([
'name' => '',
]),
]), $form->getValues());


$form->reset();

Assert::false($form->isSubmitted());
Assert::equal(ArrayHash::from([
'name' => '',
'first' => ArrayHash::from([
'name' => '',
'age' => '',
'second' => ArrayHash::from([
'name' => '',
]),
]),
'invalid' => ArrayHash::from([
'name' => '',
]),
]), $form->getValues());


$form->setDefaults([
'name' => 'new3',
]);

Assert::equal(ArrayHash::from([
'name' => 'new3',
'first' => ArrayHash::from([
'name' => '',
'age' => '',
'second' => ArrayHash::from([
'name' => '',
]),
]),
'invalid' => ArrayHash::from([
'name' => '',
]),
]), $form->getValues());

0 comments on commit d56f78c

Please sign in to comment.