File tree 2 files changed +37
-0
lines changed
2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 12
12
namespace Symfony \UX \LiveComponent \Tests \Fixtures \Component ;
13
13
14
14
use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
15
+ use Symfony \Component \Form \FormError ;
15
16
use Symfony \Component \Form \FormInterface ;
17
+ use Symfony \Component \HttpKernel \Exception \UnprocessableEntityHttpException ;
16
18
use Symfony \UX \LiveComponent \Attribute \AsLiveComponent ;
17
19
use Symfony \UX \LiveComponent \Attribute \LiveAction ;
18
20
use Symfony \UX \LiveComponent \Attribute \LiveArg ;
@@ -42,6 +44,14 @@ protected function instantiateForm(): FormInterface
42
44
return $ this ->createForm (BlogPostFormType::class, $ this ->post );
43
45
}
44
46
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
+
45
55
#[LiveAction]
46
56
public function addComment ()
47
57
{
Original file line number Diff line number Diff line change @@ -156,6 +156,33 @@ public function testFormRemembersValidationFromInitialForm(): void
156
156
;
157
157
}
158
158
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 add a new embedded comment
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
+
159
186
public function testHandleCheckboxChanges (): void
160
187
{
161
188
$ category = CategoryFixtureEntityFactory::createMany (5 );
You can’t perform that action at this time.
0 commit comments