Skip to content

Commit 2e94d55

Browse files
committed
Fixing bug where FormView was created before validation errors were removed
1 parent da9fc63 commit 2e94d55

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/LiveComponent/src/ComponentWithFormTrait.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,12 @@ private function initializeFormValues(): void
125125

126126
private function submitForm(bool $validateAll = true): void
127127
{
128+
if (null !== $this->formView) {
129+
throw new \LogicException('The submitForm() method is being called, but the FormView has already been built. Are you calling $this->getForm() - which creates the FormView - before submitting the form?');
130+
}
131+
128132
$form = $this->getFormInstance();
129133
$form->submit($this->formValues);
130-
// re-extract the "view" values in case the submitted data
131-
// changed the underlying data or structure of the form
132-
$this->formValues = $this->extractFormValues($this->getForm());
133134

134135
if ($validateAll) {
135136
// mark the entire component as validated
@@ -143,6 +144,10 @@ private function submitForm(bool $validateAll = true): void
143144
$this->clearErrorsForNonValidatedFields($form, $this->getFormName());
144145
}
145146

147+
// re-extract the "view" values in case the submitted data
148+
// changed the underlying data or structure of the form
149+
$this->formValues = $this->extractFormValues($this->getForm());
150+
146151
if (!$form->isValid()) {
147152
throw new UnprocessableEntityHttpException('Form validation failed in component');
148153
}

src/LiveComponent/tests/Fixture/Dto/BlogPost.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
namespace Symfony\UX\LiveComponent\Tests\Fixture\Dto;
1313

1414
use Symfony\Component\Validator\Constraints\Length;
15+
use Symfony\Component\Validator\Constraints\NotBlank;
1516

1617
class BlogPost
1718
{
19+
#[NotBlank(message: 'The title field should not be blank')]
1820
public $title;
1921

20-
#[Length(min: 100)]
22+
#[Length(min: 100, minMessage: 'The content field is too short')]
2123
public $content;
2224

2325
public $comments = [];

src/LiveComponent/tests/Functional/Form/ComponentWithFormTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,16 @@ public function testFormValuesRebuildAfterFormChanges(): void
5555
'headers' => ['X-CSRF-TOKEN' => $token],
5656
])
5757
->assertStatus(422)
58-
->dump()
5958
// look for original embedded form
6059
->assertContains('<textarea id="blog_post_form_comments_0_content"')
6160
// look for new embedded form
6261
->assertContains('<textarea id="blog_post_form_comments_1_content"')
6362
// changed text is still present
6463
->assertContains('changed description by user</textarea>')
6564
// check that validation happened and stuck
66-
->assertContains('This value is too short. It should have 100 characters or more.')
65+
->assertContains('The content field is too short')
66+
// make sure the title field did not suddenly become validated
67+
->assertNotContains('The title field should not be blank')
6768
->use(function (Crawler $crawler) {
6869
$div = $crawler->filter('[data-controller="live"]');
6970
$liveData = json_decode($div->attr('data-live-data-value'), true);

0 commit comments

Comments
 (0)