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

Skip to content

[Form] Add ability to clear form errors #27580

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

Merged
merged 1 commit into from
Jun 25, 2018
Merged
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
27 changes: 27 additions & 0 deletions src/Symfony/Component/Form/ClearableErrorsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form;

/**
* A form element whose errors can be cleared.
*
* @author Colin O'Dell <colinodell@gmail.com>
*/
interface ClearableErrorsInterface
{
/**
* Removes all the errors of this form.
*
* @param bool $deep Whether to remove errors from child forms as well
*/
public function clearErrors(bool $deep = false);
}
23 changes: 22 additions & 1 deletion src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
* @author Fabien Potencier <fabien@symfony.com>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class Form implements \IteratorAggregate, FormInterface
class Form implements \IteratorAggregate, FormInterface, ClearableErrorsInterface
{
/**
* The form's configuration.
Expand Down Expand Up @@ -795,6 +795,27 @@ public function getErrors($deep = false, $flatten = true)
return new FormErrorIterator($this, $errors);
}

/**
* {@inheritdoc}
*
* @return $this
*/
public function clearErrors(bool $deep = false): self
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xabbuh I have added a : self return type here per your request. See #27580 (comment) for an explanation of why I didn't also add a return type to the interface. Let me know your thoughts :)

{
$this->errors = array();

if ($deep) {
// Clear errors from children
foreach ($this as $child) {
if ($child instanceof ClearableErrorsInterface) {
$child->clearErrors(true);
}
}
}

return $this;
}

/**
* {@inheritdoc}
*/
Expand Down
42 changes: 42 additions & 0 deletions src/Symfony/Component/Form/Tests/CompoundFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,48 @@ 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->assertCount(2, $this->form->getErrors());

$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
0