File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 1212namespace Symfony \UX \LiveComponent \Tests \Fixtures \Component ;
1313
1414use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
15+ use Symfony \Component \Form \FormError ;
1516use Symfony \Component \Form \FormInterface ;
17+ use Symfony \Component \HttpKernel \Exception \UnprocessableEntityHttpException ;
1618use Symfony \UX \LiveComponent \Attribute \AsLiveComponent ;
1719use Symfony \UX \LiveComponent \Attribute \LiveAction ;
1820use 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 {
Original file line number Diff line number Diff 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 );
You can’t perform that action at this time.
0 commit comments