Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5c1a657

Browse files
committed
[Form] Add ability to clear form errors
1 parent 5c338cc commit 5c1a657

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form;
13+
14+
/**
15+
* A form element whose errors can be cleared.
16+
*
17+
* @author Colin O'Dell <[email protected]>
18+
*/
19+
interface ClearableErrorsInterface
20+
{
21+
/**
22+
* Removes all the errors of this form.
23+
*
24+
* @param bool $deep Whether to remove errors from child forms as well
25+
*
26+
* @return $this
27+
*/
28+
public function clearErrors(bool $deep = false);
29+
}

src/Symfony/Component/Form/Form.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
* @author Fabien Potencier <[email protected]>
5959
* @author Bernhard Schussek <[email protected]>
6060
*/
61-
class Form implements \IteratorAggregate, FormInterface
61+
class Form implements \IteratorAggregate, FormInterface, ClearableErrorsInterface
6262
{
6363
/**
6464
* The form's configuration.
@@ -795,6 +795,25 @@ public function getErrors($deep = false, $flatten = true)
795795
return new FormErrorIterator($this, $errors);
796796
}
797797

798+
/**
799+
* {@inheritdoc}
800+
*/
801+
public function clearErrors(bool $deep = false)
802+
{
803+
$this->errors = array();
804+
805+
if ($deep) {
806+
// Clear errors from children
807+
foreach ($this as $child) {
808+
if ($child instanceof ClearableErrorsInterface) {
809+
$child->clearErrors(true);
810+
}
811+
}
812+
}
813+
814+
return $this;
815+
}
816+
798817
/**
799818
* {@inheritdoc}
800819
*/

src/Symfony/Component/Form/Tests/CompoundFormTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,48 @@ public function testGetErrorsDeepRecursive()
879879
$this->assertSame($nestedError, $nestedErrorsAsArray[0]);
880880
}
881881

882+
public function testClearErrors()
883+
{
884+
$this->form->addError(new FormError('Error 1'));
885+
$this->form->addError(new FormError('Error 2'));
886+
887+
$this->assertCount(2, $this->form->getErrors());
888+
889+
$this->form->clearErrors();
890+
891+
$this->assertCount(0, $this->form->getErrors());
892+
}
893+
894+
public function testClearErrorsShallow()
895+
{
896+
$this->form->addError($error1 = new FormError('Error 1'));
897+
$this->form->addError($error2 = new FormError('Error 2'));
898+
899+
$childForm = $this->getBuilder('Child')->getForm();
900+
$childForm->addError(new FormError('Nested Error'));
901+
$this->form->add($childForm);
902+
903+
$this->form->clearErrors(false);
904+
905+
$this->assertCount(0, $this->form->getErrors(false));
906+
$this->assertCount(1, $this->form->getErrors(true));
907+
}
908+
909+
public function testClearErrorsDeep()
910+
{
911+
$this->form->addError($error1 = new FormError('Error 1'));
912+
$this->form->addError($error2 = new FormError('Error 2'));
913+
914+
$childForm = $this->getBuilder('Child')->getForm();
915+
$childForm->addError($nestedError = new FormError('Nested Error'));
916+
$this->form->add($childForm);
917+
918+
$this->form->clearErrors(true);
919+
920+
$this->assertCount(0, $this->form->getErrors(false));
921+
$this->assertCount(0, $this->form->getErrors(true));
922+
}
923+
882924
// Basic cases are covered in SimpleFormTest
883925
public function testCreateViewWithChildren()
884926
{

0 commit comments

Comments
 (0)