@@ -3,9 +3,9 @@ Squirrel Validator Cascade
33
44[ ![ Build Status] ( https://img.shields.io/travis/com/squirrelphp/validator-cascade.svg )] ( https://travis-ci.com/squirrelphp/validator-cascade ) [ ![ Test Coverage] ( https://api.codeclimate.com/v1/badges/e056be025c6db0eb31f1/test_coverage )] ( https://codeclimate.com/github/squirrelphp/validator-cascade/test_coverage ) ![ PHPStan] ( https://img.shields.io/badge/style-level%208-success.svg?style=flat-round&label=phpstan ) [ ![ Packagist Version] ( https://img.shields.io/packagist/v/squirrelphp/validator-cascade.svg?style=flat-round )] ( https://packagist.org/packages/squirrelphp/validator-cascade ) [ ![ PHP Version] ( https://img.shields.io/packagist/php-v/squirrelphp/validator-cascade.svg )] ( https://packagist.org/packages/squirrelphp/validator-cascade ) [ ![ Software License] ( https://img.shields.io/badge/license-MIT-success.svg?style=flat-round )] ( LICENSE )
55
6- Reimplements the ` Valid ` constraint in the Symfony validator component as ` Cascade ` annotation which is more straightforward to use than ` Valid ` and has no surprising behavior.
6+ Reimplements the ` Valid ` constraint in the Symfony validator component as ` Cascade ` annotation/attribute which is more straightforward to use than ` Valid ` and has no surprising behavior.
77
8- This component is compatible with the Symfony validator component in versions 4.0+ and 5.0+ and will be adapted to support future versions of Symfony (if any changes are necessary for that).
8+ This component is compatible with the Symfony validator component in version 5.0+ and will be adapted to support future versions of Symfony (if any changes are necessary for that).
99
1010Installation
1111------------
@@ -53,62 +53,106 @@ class Order
5353 *
5454 * Validates "Default" and "phoneNumberMandatory" validation groups in $shippingAddress
5555 *
56- * @Assert\NotNull()
57- * @Assert\Type(type="Address")
5856 * @Cascade(trigger={"Default", "phoneNumberMandatory"})
5957 */
60- public $shippingAddress;
58+ public Address $shippingAddress;
6159
6260 /**
6361 * Validate $invoiceAddress only if validation group
6462 * "alternateInvoiceAddress" is passed to validator
6563 *
6664 * Validates only "Default" validation group in $invoiceAddress, so phone number is optional
6765 *
68- * @Assert\NotNull()
69- * @Assert\Type(type="Address")
66+ * @Assert\NotNull(groups={"alternateInvoiceAddress"})
7067 * @Cascade(groups={"alternateInvoiceAddress"})
7168 */
72- public $invoiceAddress;
69+ public ?Address $invoiceAddress = null ;
7370}
7471
7572class Address
7673{
7774 /**
78- * @Assert\NotNull()
79- * @Assert\NotBlank()
8075 * @Assert\Length(
8176 * min = 1,
8277 * max = 50
8378 * )
84- *
85- * @var string
8679 */
87- public $street = '';
80+ public string $street = '';
8881
8982 /**
90- * @Assert\NotNull()
91- * @Assert\NotBlank()
9283 * @Assert\Length(
9384 * min = 1,
9485 * max = 50
9586 * )
96- *
97- * @var string
9887 */
99- public $city = '';
88+ public string $city = '';
10089
10190 /**
102- * @Assert\NotNull(groups={"phoneNumberMandatory"})
103- * @Assert\NotBlank(groups={"phoneNumberMandatory"})
10491 * @Assert\Length(
10592 * min = 1,
106- * max = 50
93+ * max = 50,
94+ * groups = {"phoneNumberMandatory"}
10795 * )
10896 *
10997 * @var string
11098 */
111- public $phoneNumber = '';
99+ public string $phoneNumber = '';
100+ }
101+
102+ $order = new Order();
103+ $order->shippingAddress = new Address();
104+ $order->invoiceAddress = new Address();
105+
106+ // This validates with the "Default" validation group,
107+ // so only shippingAddress must be specified
108+ $symfonyValidator->validate($order);
109+
110+ // This also validates the invoice address in addition
111+ // to the shipping address
112+ $symfonyValidator->validate($order, null, [
113+ "Default",
114+ "alternateInvoiceAddress",
115+ ]);
116+ ```
117+
118+ If you are using PHP8+, you should use attributes instead (although annotations will still work):
119+
120+ ``` php
121+ use Squirrel\ValidatorCascade\Cascade;
122+ use Symfony\Component\Validator\Constraints as Assert;
123+
124+ class Order
125+ {
126+ /**
127+ * Validate $shippingAddress if validation with no validation
128+ * group or the "Default" validation group is triggered
129+ *
130+ * Validates "Default" and "phoneNumberMandatory" validation groups in $shippingAddress
131+ */
132+ #[Cascade(trigger: ['Default', 'phoneNumberMandatory'])]
133+ public Address $shippingAddress;
134+
135+ /**
136+ * Validate $invoiceAddress only if validation group
137+ * "alternateInvoiceAddress" is passed to validator
138+ *
139+ * Validates only "Default" validation group in $invoiceAddress, so phone number is optional
140+ */
141+ #[Assert\NotNull(groups: ['alternateInvoiceAddress'])]
142+ #[Cascade(groups: ['alternateInvoiceAddress'])]
143+ public ?Address $invoiceAddress = null;
144+ }
145+
146+ class Address
147+ {
148+ #[Assert\Length(min: 1, max: 50)]
149+ public string $street = '';
150+
151+ #[Assert\Length(min: 1, max: 50)]
152+ public string $city = '';
153+
154+ #[Assert\Length(min: 1, max: 50, groups: ['phoneNumberMandatory'])]
155+ public string $phoneNumber = '';
112156}
113157
114158$order = new Order();
0 commit comments