PHPackages                             squirrelphp/validator-cascade - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. squirrelphp/validator-cascade

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

squirrelphp/validator-cascade
=============================

Cascade attribute for Symfony Validator, reimplementing the Valid constraint in a more flexible and understandable way

v3.3(7mo ago)645.3k↓72.6%1MITPHPPHP &gt;=8.4

Since Aug 9Pushed 7mo ago2 watchersCompare

[ Source](https://github.com/squirrelphp/validator-cascade)[ Packagist](https://packagist.org/packages/squirrelphp/validator-cascade)[ Docs](https://github.com/squirrelphp/validator-cascade)[ RSS](/packages/squirrelphp-validator-cascade/feed)WikiDiscussions master Synced yesterday

READMEChangelog (9)Dependencies (7)Versions (10)Used By (0)

Squirrel Validator Cascade
==========================

[](#squirrel-validator-cascade)

[![Build Status](https://camo.githubusercontent.com/f36e2bb2e5067a7d93788906532859dc0ee3a7999c7a9f4e8bc282a846aee713/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636f6d2f737175697272656c7068702f76616c696461746f722d636173636164652e737667)](https://travis-ci.com/squirrelphp/validator-cascade) [![Test Coverage](https://camo.githubusercontent.com/1831520fefa1a3a064495a6fcac50a0a0ad69c9cbbb5276971063c579a90910c/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f65303536626530323563366462306562333166312f746573745f636f766572616765)](https://codeclimate.com/github/squirrelphp/validator-cascade/test_coverage) [![PHPStan](https://camo.githubusercontent.com/65e2c93fbe63f94af93706ba48d15eafa85a0cd4ff0c5aeea712b4da080d7545/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374796c652d6c6576656c253230382d737563636573732e7376673f7374796c653d666c61742d726f756e64266c6162656c3d7068707374616e)](https://camo.githubusercontent.com/65e2c93fbe63f94af93706ba48d15eafa85a0cd4ff0c5aeea712b4da080d7545/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374796c652d6c6576656c253230382d737563636573732e7376673f7374796c653d666c61742d726f756e64266c6162656c3d7068707374616e) [![Packagist Version](https://camo.githubusercontent.com/620d0ec925c9a209849abea067c6aeccee8eec8cf3bae094ba36051bc3b981f1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f737175697272656c7068702f76616c696461746f722d636173636164652e7376673f7374796c653d666c61742d726f756e64)](https://packagist.org/packages/squirrelphp/validator-cascade) [![PHP Version](https://camo.githubusercontent.com/ec6405d464d60117368601d9e92a8710053b0d45d672908a137b12e64b7878a1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f737175697272656c7068702f76616c696461746f722d636173636164652e737667)](https://packagist.org/packages/squirrelphp/validator-cascade) [![Software License](https://camo.githubusercontent.com/4f9c8f8c009336d7177cf697d694e2e763e5e707f1a6224ab0d295465eadffbd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d737563636573732e7376673f7374796c653d666c61742d726f756e64)](LICENSE)

Reimplements the `Valid` constraint in the Symfony validator component as `Cascade` attribute which is more straightforward to use than `Valid` and has no surprising behavior.

This component is compatible with the Symfony validator component in version 5.x (v2.x with annotation/attribute support) and 6.x/7.x (v3.x with only attribute support) and will be adapted to support future versions of Symfony (if any changes are necessary for that).

Installation
------------

[](#installation)

```
composer require squirrelphp/validator-cascade

```

Table of contents
-----------------

[](#table-of-contents)

- [Usage](#usage)
- [Example](#example)
- [Why not use the Valid constraint?](#why-not-use-the-valid-constraint)

Usage
-----

[](#usage)

`Cascade` is a constraint validation which makes sure an object or an array of objects are validated by the Symfony validator component, so it cascades validation.

There are only two options:

- `groups` defines to which validation groups the `Cascade` constraint belongs to, with the same behavior as any regular validator constraint. If you do not define `groups` it is set to `Default`. The `Cascade` constraint is only executed if one of the validation groups matches.
- `trigger` defines which validation groups to trigger on the child object(s). By default only `Default` is triggered, so if you want any other validation groups to trigger you have to specify them with `trigger`. The validation groups of the "parent" are never cascaded.

That is it!

Example
-------

[](#example)

Below is a common example in real applications: You might have an order and multiple possible addresses for the order (one for shipping, one for invoice) with different requirements, and some addresses should be optional, but if they are specified they should still be validated.

`$shippingAddress` shows how to trigger specific validation groups in the child object, in this case to make the phone number a mandatory part of the information (often the case for shipping, but usually not necessary for other uses) in addition to the "Default" constraints.

`$invoiceAddress` is only validated if the validation group "alternateInvoiceAddress" is passed to the validator (which could be done if the user selected an option like "choose different invoice address"). The phone number is optional, as we do not pass the `trigger` option so only the Default group is validated in the Adress object.

```
use Squirrel\ValidatorCascade\Cascade;
use Symfony\Component\Validator\Constraints as Assert;

class Order
{
    /**
     * Validate $shippingAddress if validation with no validation
     * group or the "Default" validation group is triggered
     *
     * Validates "Default" and "phoneNumberMandatory" validation groups in $shippingAddress
     */
    #[Cascade(trigger: ['Default', 'phoneNumberMandatory'])]
    public Address $shippingAddress;

    /**
     * Validate $invoiceAddress only if validation group
     * "alternateInvoiceAddress" is passed to validator
     *
     * Validates only "Default" validation group in $invoiceAddress, so phone number is optional
     */
    #[Assert\NotNull(groups: ['alternateInvoiceAddress'])]
    #[Cascade(groups: ['alternateInvoiceAddress'])]
    public ?Address $invoiceAddress = null;
}

class Address
{
    #[Assert\Length(min: 1, max: 50)]
    public string $street = '';

    #[Assert\Length(min: 1, max: 50)]
    public string $city = '';

    #[Assert\Length(min: 1, max: 50, groups: ['phoneNumberMandatory'])]
    public string $phoneNumber = '';
}

$order = new Order();
$order->shippingAddress = new Address();
$order->invoiceAddress = new Address();

// This validates with the "Default" validation group,
// so only shippingAddress must be specified
$symfonyValidator->validate($order);

// This also validates the invoice address in addition
// to the shipping address
$symfonyValidator->validate($order, null, [
    "Default",
    "alternateInvoiceAddress",
]);
```

Why not use the `Valid` constraint?
-----------------------------------

[](#why-not-use-the-valid-constraint)

The current implementation of the `Valid` constraint in the Symfony validator component has severe limitations when it comes to validation groups and behaves differently than any other constraint:

### `Valid` constraint without validation group

[](#valid-constraint-without-validation-group)

```
#[Assert\Valid]
public $someobject;
```

The above code looks like a regular assertion, but it behaves differently:

- The assertion is always executed, no matter what validation group you give to the validator
- The assertion therefore does not belong to the "Default" group

This is fine for simple objects or when you don't need any validation groups at all, but it is still different from any other assertion, as you cannot "skip" this constraint even if you later add validation groups.

### `Valid` constraint with validation group(s)

[](#valid-constraint-with-validation-groups)

```
#[Assert\Valid(groups: ['invoice'])]
public $someobject;
```

The `Valid` assertion above only triggers when you validate the "invoice" validation group, which is what you would expect. Yet there is plenty of unexpected behavior:

- It only triggers the validation group "invoice" in $someobject, no other validation groups are passed to the object (if, for example, you are validating the groups "Default" and "invoice" the group "Default" never reaches $someobject, only "invoice")
- There is no way to change which validation groups are triggered in $someobject
- The "traverse" option for `Valid` is not used when a validation group is defined. Although the "traverse" option should probably not be used or needed in general

Having validation groups both as a trigger and as a filter severly limits how you can use it, and makes most use cases (like our [example with addresses](#example)) impossible to do with `Valid`. Even if you manage to make it work, your code will not be self explanatory and it is easy to make mistakes or misunderstand the attributes.

`Cascade` as defined in this component separates which validation group the constraint belongs to and which validation groups are triggered in the child object(s). What it cannot do is cascade the validation groups of the parent to the child object, as this information is only available in the `RecursiveContextualValidator` class of the validator component and cannot be accessed without changing a lot of the internals of the validator component (unfortunately).

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance64

Regular maintenance activity

Popularity33

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity82

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 94.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~288 days

Recently: every ~361 days

Total

9

Last Release

216d ago

Major Versions

v1.2 → v2.02020-12-19

v2.1 → v3.02021-12-16

PHP version history (5 changes)v1.0PHP ^7.2

v1.2PHP &gt;=7.2

v2.0PHP &gt;=7.4

v3.0PHP &gt;=8.0

v3.3PHP &gt;=8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/8cd200dc74af2d77e8c480000a7e3bab0b8a98fae4b399d306b3d332354f8177?d=identicon)[iquito\_ch](/maintainers/iquito_ch)

---

Top Contributors

[![iquito](https://avatars.githubusercontent.com/u/973653?v=4)](https://github.com/iquito "iquito (16 commits)")[![zing-james](https://avatars.githubusercontent.com/u/68030205?v=4)](https://github.com/zing-james "zing-james (1 commits)")

---

Tags

phpsymfonyvalidatorformvalidcascade

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/squirrelphp-validator-cascade/health.svg)

```
[![Health](https://phpackages.com/badges/squirrelphp-validator-cascade/health.svg)](https://phpackages.com/packages/squirrelphp-validator-cascade)
```

###  Alternatives

[barbieswimcrew/zip-code-validator

Constraint class for international zipcode validation

772.5M](/packages/barbieswimcrew-zip-code-validator)[rcsofttech/audit-trail-bundle

Enterprise-grade, high-performance Symfony audit trail bundle. Automatically track Doctrine entity changes with split-phase architecture, multiple transports (HTTP, Queue, Doctrine), and sensitive data masking.

1189.8k](/packages/rcsofttech-audit-trail-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
