PHP code example of squirrelphp / validator-cascade
1. Go to this page and download the library: Download squirrelphp/validator-cascade library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
squirrelphp / validator-cascade example snippets
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",
]);
#[Assert\Valid]
public $someobject;
#[Assert\Valid(groups: ['invoice'])]
public $someobject;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.