Skip to content

Commit 9a39daa

Browse files
committed
[Live] Adding helper to reset validation state
1 parent 1620116 commit 9a39daa

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

src/LiveComponent/doc/index.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,30 @@ Once a component has been validated, the component will "remember" that
20302030
it has been validated. This means that, if you edit a field and the
20312031
component re-renders, it will be validated again.
20322032

2033+
Resetting Validation Errors
2034+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
2035+
2036+
If you want to clear validation errors (e.g. so you can reuse the form again),
2037+
you can call the ``resetValidation()`` method::
2038+
2039+
// ...
2040+
class EditUser
2041+
{
2042+
// ...
2043+
2044+
#[LiveAction]
2045+
public function save()
2046+
{
2047+
// validate, save, etc
2048+
2049+
// reset your live props to the original state
2050+
$this->user = new User();
2051+
$this->agreeToTerms = false;
2052+
// clear the validation state
2053+
$this->resetValidation();
2054+
}
2055+
}
2056+
20332057
Real-Time Validation on Change
20342058
------------------------------
20352059

src/LiveComponent/src/ValidatableComponentTrait.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ public function setComponentValidator(ComponentValidatorInterface $componentVali
152152
$this->componentValidator = $componentValidator;
153153
}
154154

155+
private function resetValidation(): void
156+
{
157+
$this->isValidated = false;
158+
$this->validatedFields = [];
159+
$this->validationErrors = null;
160+
}
161+
155162
private function getValidator(): ComponentValidatorInterface
156163
{
157164
if (!$this->componentValidator) {

src/LiveComponent/tests/Fixtures/Component/ValidatingComponent.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Symfony\Component\Validator\Constraints\Length;
66
use Symfony\Component\Validator\Constraints\NotBlank;
77
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
8+
use Symfony\UX\LiveComponent\Attribute\LiveAction;
89
use Symfony\UX\LiveComponent\Attribute\LiveProp;
910
use Symfony\UX\LiveComponent\DefaultActionTrait;
1011
use Symfony\UX\LiveComponent\ValidatableComponentTrait;
@@ -19,4 +20,10 @@ final class ValidatingComponent
1920
#[NotBlank]
2021
#[Length(min: 3)]
2122
public string $name = '';
23+
24+
#[LiveAction]
25+
public function resetValidationAction(): void
26+
{
27+
$this->resetValidation();
28+
}
2229
}

src/LiveComponent/tests/Functional/ValidatableComponentTraitTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,15 @@ public function testFormValuesRebuildAfterFormChanges(): void
5959
->assertSuccessful()
6060
->assertContains('Has Error: yes')
6161
;
62+
63+
$browser
64+
->post('/_components/validating_component/resetValidationAction', [
65+
'json' => [
66+
'props' => $dehydratedProps,
67+
],
68+
])
69+
->assertSuccessful()
70+
->assertContains('Has Error: no')
71+
;
6272
}
6373
}

0 commit comments

Comments
 (0)