定制 squirrelphp/validator-cascade 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

Build Status Test Coverage PHPStan Packagist Version PHP Version Software 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

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:

  • 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

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 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) 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 squirrelphp/validator-cascade 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 45.31k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-08-09