squirrelphp/validator-cascade
Composer 安装命令:
composer require squirrelphp/validator-cascade
包简介
Cascade attribute for Symfony Validator, reimplementing the Valid constraint in a more flexible and understandable way
README 文档
README
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
composer require squirrelphp/validator-cascade
Table of contents
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:
-
groupsdefines to which validation groups theCascadeconstraint belongs to, with the same behavior as any regular validator constraint. If you do not definegroupsit is set toDefault. TheCascadeconstraint is only executed if one of the validation groups matches. -
triggerdefines which validation groups to trigger on the child object(s). By default onlyDefaultis triggered, so if you want any other validation groups to trigger you have to specify them withtrigger. The validation groups of the "parent" are never cascaded.
That is it!
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?
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
#[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)
#[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
Validis 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) 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).
squirrelphp/validator-cascade 适用场景与选型建议
squirrelphp/validator-cascade 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 45.31k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2019 年 08 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「php」 「form」 「validator」 「valid」 「cascade」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 squirrelphp/validator-cascade 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 squirrelphp/validator-cascade 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 squirrelphp/validator-cascade 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The bundle for easy using json-rpc api on your project
Diese Contao 4 Erweiterung stellt Google reCAPTCHA V2 in Form eines neuen Formularfeldes im Formulargenerator bereit. This extension provides Google reCAPTCHA V2 in the form of a new form field in the form generator of Contao Open Source CMS.
A Laravel Filament Forms slug field.
Bundle Symfony DaplosBundle
A jQuery augmented PHP library for creating and validating HTML forms
Laravel contact us form package to send email and save to database
统计信息
- 总下载量: 45.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-08-09