@@ -180,9 +180,10 @@ use Symfony\UX\LiveComponent\Attribute\LiveProp;
180
180
#[AsLiveComponent('random_number')]
181
181
class RandomNumberComponent
182
182
{
183
- /** @ LiveProp */
183
+ #[ LiveProp]
184
184
public int $min = 0;
185
- /** @LiveProp */
185
+
186
+ #[LiveProp]
186
187
public int $max = 1000;
187
188
188
189
public function getRandomNumber(): string
@@ -201,14 +202,14 @@ when rendering the component:
201
202
{{ component('random_number', { min: 5, max: 500 }) }}
202
203
```
203
204
204
- But what's up with those ` @ LiveProp` annotations ? A property with
205
- the ` @ LiveProp` annotation (or ` LiveProp ` PHP 8 attribute) becomes
206
- a "stateful" property for this component. In other words, each time
207
- we click the "Generate a new number!" button, when the component
208
- re-renders, it will _ remember_ the original values for the ` $min ` and
209
- ` $max ` properties and generate a random number between 5 and 500.
210
- If you forgot to add ` @ LiveProp` , when the component re-rendered,
211
- those two values would _ not_ be set on the object.
205
+ But what's up with those ` LiveProp ` attributes ? A property with
206
+ the ` LiveProp ` attribute becomes a "stateful" property for this
207
+ component. In other words, each time we click the "Generate a
208
+ new number!" button, when the component re-renders, it will
209
+ _ remember_ the original values for the ` $min ` and ` $max ` properties
210
+ and generate a random number between 5 and 500. If you forgot to
211
+ add ` LiveProp ` , when the component re-rendered, those two values
212
+ would _ not_ be set on the object.
212
213
213
214
In short: LiveProps are "stateful properties": they will always
214
215
be set when rendering. Most properties will be LiveProps, with
@@ -264,11 +265,12 @@ the `writable=true` option:
264
265
265
266
class RandomNumberComponent
266
267
{
267
- - /** @ LiveProp() */
268
- + /** @ LiveProp(writable= true) */
268
+ - #[ LiveProp]
269
+ + #[ LiveProp(writable: true)]
269
270
public int $min = 0;
270
- - /** @LiveProp() */
271
- + /** @LiveProp(writable=true) */
271
+
272
+ - #[LiveProp]
273
+ + #[LiveProp(writable: true)]
272
274
public int $max = 1000;
273
275
274
276
// ...
@@ -423,8 +425,8 @@ want to add a "Reset Min/Max" button to our "random number"
423
425
component that, when clicked, sets the min/max numbers back
424
426
to a default value.
425
427
426
- First, add a method with a ` LiveAction ` annotation (or PHP 8 attribute)
427
- above it that does the work:
428
+ First, add a method with a ` LiveAction ` attribute above it that
429
+ does the work:
428
430
429
431
``` php
430
432
// src/Components/RandomNumberComponent.php
@@ -437,9 +439,7 @@ class RandomNumberComponent
437
439
{
438
440
// ...
439
441
440
- /**
441
- * @LiveAction
442
- */
442
+ #[LiveAction]
443
443
public function resetMinMax()
444
444
{
445
445
$this->min = 0;
@@ -502,9 +502,7 @@ class RandomNumberComponent
502
502
{
503
503
// ...
504
504
505
- /**
506
- * @LiveAction
507
- */
505
+ #[LiveAction]
508
506
public function resetMinMax(LoggerInterface $logger)
509
507
{
510
508
$this->min = 0;
@@ -547,9 +545,7 @@ class RandomNumberComponent extends AbstractController
547
545
{
548
546
// ...
549
547
550
- /**
551
- * @LiveAction
552
- */
548
+ #[LiveAction]
553
549
public function resetMinMax()
554
550
{
555
551
// ...
@@ -698,9 +694,8 @@ class PostFormComponent extends AbstractController
698
694
* the form renders fields with names like `name="post[title]"`.
699
695
* We set fieldName="" so that this live prop doesn't collide
700
696
* with that data. The value - initialFormData - could be anything.
701
- *
702
- * @LiveProp(fieldName="initialFormData")
703
697
*/
698
+ #[LiveProp(fieldName: 'initialFormData')]
704
699
public ?Post $post = null;
705
700
706
701
/**
@@ -870,9 +865,7 @@ class PostFormComponent extends AbstractController
870
865
{
871
866
// ...
872
867
873
- /**
874
- * @LiveAction()
875
- */
868
+ #[LiveAction]
876
869
public function save(EntityManagerInterface $entityManager)
877
870
{
878
871
// shortcut to submit the form with form values
@@ -929,9 +922,7 @@ use Symfony\UX\LiveComponent\Attribute\LiveProp;
929
922
#[AsLiveComponent('edit_post')]
930
923
class EditPostComponent
931
924
{
932
- /**
933
- * @LiveProp()
934
- */
925
+ #[LiveProp]
935
926
public Post $post;
936
927
}
937
928
```
@@ -974,10 +965,8 @@ you can enable it via the `exposed` option:
974
965
975
966
class EditPostComponent
976
967
{
977
- /**
978
- - * @LiveProp(exposed={})
979
- + * @LiveProp(exposed={"title", "content"})
980
- */
968
+ - #[LiveProp]
969
+ + #[LiveProp(exposed: ['title', 'content'])]
981
970
public Post $post;
982
971
983
972
// ...
@@ -1017,21 +1006,17 @@ class EditUserComponent
1017
1006
{
1018
1007
use ValidatableComponentTrait;
1019
1008
1020
- /**
1021
- * @LiveProp(exposed={"email", "plainPassword"})
1022
- * @Assert\Valid()
1023
- */
1009
+ #[LiveProp(exposed: ['email', 'plainPassword'])]
1010
+ #[Assert\Valid]
1024
1011
public User $user;
1025
1012
1026
- /**
1027
- * @LiveProp()
1028
- * @Assert\IsTrue()
1029
- */
1013
+ #[LiveProp]
1014
+ #[Assert\IsTrue]
1030
1015
public bool $agreeToTerms = false;
1031
1016
}
1032
1017
```
1033
1018
1034
- Be sure to add the ` @ Assert\IsValid` to any property where you want
1019
+ Be sure to add the ` Assert\IsValid ` to any property where you want
1035
1020
the object on that property to also be validated.
1036
1021
1037
1022
Thanks to this setup, the component will now be automatically validated
@@ -1051,9 +1036,7 @@ class EditUserComponent
1051
1036
{
1052
1037
// ...
1053
1038
1054
- /**
1055
- * @LiveAction()
1056
- */
1039
+ #[LiveAction]
1057
1040
public function save()
1058
1041
{
1059
1042
// this will throw an exception if validation fails
0 commit comments