8000 [Form] Added ability to clear form errors by colinodell · Pull Request #14233 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Added ability to clear form errors #14233

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

Closed
wants to merge 1 commit into from
Closed
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: 2 additions & 0 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ UPGRADE FROM 2.x to 3.0

### Form

* The method `clearErrors()` was added to `FormInterface`.

* The option "precision" was renamed to "scale".

Before:
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Form/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ public function getErrors($deep = false, $flatten = true)
return new FormErrorIterator($this, array());
}

/**
* {@inheritdoc}
*/
public function clearErrors($deep = false)
{
return $this;
}

/**
* Unsupported method.
*
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
* removed `AbstractTypeExtension::setDefaultOptions()` method
* added `FormTypeInterface::configureOptions()` method
* added `FormTypeExtensionInterface::configureOptions()` method
* added `FormInterface::clearErrors()` method

2.7.0
-----
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,23 @@ public function getErrors($deep = false, $flatten = true)
return new FormErrorIterator($this, $errors);
}

/**
* {@inheritdoc}
*/
public function clearErrors($deep = false)
{
$this->errors = array();

// Copy the errors of nested forms to the $errors array
if ($deep) {
foreach ($this as $child) {
$child->clearErrors(true);
}
}

return $this;
}

/**
* Returns a string representation of all form errors (including children errors).
*
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Form/FormInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ public function all();
*/
public function getErrors($deep = false, $flatten = true);

/**
* Removes all the errors of this form.
*
* @param bool $deep Whether to remove errors of child forms as well
*
* @return FormInterface The form instance
*/
public function clearErrors($deep = false);

/**
* Updates the form with default data.
*
Expand Down
40 changes: 40 additions & 0 deletions src/Symfony/Component/Form/Tests/CompoundFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,46 @@ public function testGetErrorsDeepRecursive()
$this->assertSame($nestedError, $nestedErrorsAsArray[0]);
}

public function testClearErrors()
{
$this->form->addError(new FormError('Error 1'));
$this->form->addError(new FormError('Error 2'));

$this->form->clearErrors();

$this->assertCount(0, $this->form->getErrors());
}

public function testClearErrorsShallow()
{
$this->form->addError($error1 = new FormError('Error 1'));
$this->form->addError($error2 = new FormError('Error 2'));

$childForm = $this->getBuilder('Child')->getForm();
$childForm->addError(new FormError('Nested Error'));
$this->form->add($childForm);

$this->form->clearErrors(false);

$this->assertCount(0, $this->form->getErrors(false));
$this->assertCount(1, $this->form->getErrors(true));
}

public function testClearErrorsDeep()
{
$this->form->addError($error1 = new FormError('Error 1'));
$this->form->addError($error2 = new FormError('Error 2'));

$childForm = $this->getBuilder('Child')->getForm();
$childForm->addError($nestedError = new FormError('Nested Error'));
$this->form->add($childForm);

$this->form->clearErrors(true);

$this->assertCount(0, $this->form->getErrors(false));
$this->assertCount(0, $this->form->getErrors(true));
}

// Basic cases are covered in SimpleFormTest
public function testCreateViewWithChildren()
{
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Form/Tests/SimpleFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,14 @@ public function testHasNoErrors()
$this->assertCount(0, $this->form->getErrors());
}

public function testClearErrors()
{
$this->form->addError(new FormError('Error!'));
$this->form->clearErrors();

$this->assertCount(0, $this->form->getErrors());
}

/**
* @expectedException \Symfony\Component\Form\Exception\AlreadySubmittedException
*/
Expand Down
0