Skip to content

Commit 1297e56

Browse files
author
Maurice Niedergesäß
committed
Added TestCase asserting that manually added FormError is rendered in template.
1 parent c6189b4 commit 1297e56

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component;
1313

1414
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
15+
use Symfony\Component\Form\FormError;
1516
use Symfony\Component\Form\FormInterface;
17+
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
1618
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
1719
use Symfony\UX\LiveComponent\Attribute\LiveAction;
1820
use Symfony\UX\LiveComponent\Attribute\LiveArg;
@@ -42,6 +44,14 @@ protected function instantiateForm(): FormInterface
4244
return $this->createForm(BlogPostFormType::class, $this->post);
4345
}
4446

47+
#[LiveAction]
48+
public function submitAndAddErrorToForm(): void
49+
{
50+
$this->submitForm();
51+
$this->getForm()->addError(new FormError("manually added form error"));
52+
throw new UnprocessableEntityHttpException();
53+
}
54+
4555
#[LiveAction]
4656
public function addComment()
4757
{

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

+27
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,33 @@ public function testFormRemembersValidationFromInitialForm(): void
156156
;
157157
}
158158

159+
public function testFormViewSynchronizesWithFormInstance(): void
160+
{
161+
/** @var FormFactoryInterface $formFactory */
162+
$formFactory = self::getContainer()->get('form.factory');
163+
164+
$form = $formFactory->create(BlogPostFormType::class);
165+
// make sure validation does not fail on content constraint (min 100 characters)
166+
$validContent = implode('a', range(0, 100));
167+
$form->submit(['title' => 'Title', 'content' => $validContent]);
168+
169+
$mounted = $this->mountComponent('form_with_collection_type', [
170+
'form' => $form->createView(),
171+
]);
172+
$dehydratedProps = $this->dehydrateComponent($mounted)->getProps();
173+
174+
$this->browser()
175+
// post to action, which will manually add a FormError to the FormInstance after submit
176+
->post('/_components/form_with_collection_type/submitAndAddErrorToForm', [
177+
'body' => ['data' => json_encode(['props' => $dehydratedProps])],
178+
])
179+
// action always throws 422
180+
->assertStatus(422)
181+
// assert manually added error within LiveAction after submit is rendered in template
182+
->assertContains('manually added form error')
183+
;
184+
}
185+
159186
public function testHandleCheckboxChanges(): void
160187
{
161188
$category = CategoryFixtureEntityFactory::createMany(5);

0 commit comments

Comments
 (0)