CakePHP 3 - 使用可重用的验证器

2021-12-21 00:00:00 validation php cakephp

在 CakePHP 3 文档中有一个关于可重用验证器的部分:https://book.cakephp.org/3.0/en/core-libraries/validation.html#creating-reusable-validators

In the CakePHP 3 docs there's a section on Reusable Validators: https://book.cakephp.org/3.0/en/core-libraries/validation.html#creating-reusable-validators

它没有说明你如何使用它们,但在控制器中.谁能举个例子?

It doesn't say how you use them, in a Controller though. Can anyone give an example?

我有一个允许上传 .csv 文件的特定应用程序.应用程序中 .csv 文件的验证始终相同:检查其 MIME 类型、检查大小、检查扩展名等.

I have a particular application which allows .csv files to be uploaded. The validation for a .csv file in the application is always the same: check it's MIME type, check the size, check the extension, etc.

所以我的计划是将其实现为可重复使用的验证器 - 对吗?

So my plan was to implement this as a reusable validator - is that correct?

我有一个带有 upload() 函数的 UploadsController.php,我想用它来验证来自表单的数据.我很困惑,因为此时我没有创建实体 - 而只是尝试验证我的文件 - 所以文档中的所有这些 patchEntity() 内容在这里没有任何意义.

I have an UploadsController.php with an upload() function, which is where I want to use this to validate data coming from a form. I'm confused because I'm not creating an Entity at this point - but rather just trying to validate my file - so all this patchEntity() stuff in the docs makes no sense here.

我发现有关 Cake 3 验证的文档非常混乱,因为 ORM 下有一个部分 (https://book.cakephp.org/3.0/en/orm/validation.html) 上面写着

I find the documentation on validation for Cake 3 very confusing, because there's a section under the ORM (https://book.cakephp.org/3.0/en/orm/validation.html) where it says

验证规则在Table类

但后来,它有一个完全不同的部分来验证实体(https://book.cakephp.org/3.0/en/core-libraries/validation.html#validating-entities).

But later on, it has a completely different section on validating Entities (https://book.cakephp.org/3.0/en/core-libraries/validation.html#validating-entities).

然后我们有可重用的验证器......以及其他各种东西.

Then we have Reusable Validators..... and various other things.

由于 Cake 3 中的 Table 和 Entity 模型类不同,有人能解释一下您将如何验证文件上传之类的东西,特别是考虑到甚至可能根本没有涉及表?

Since Table and Entity model classes in Cake 3 differ, can someone explain how you would go about validating something like a file upload, particularly given that there may even be no table at all involved?

如果您在表单上组合使用两者可重用验证器(用于验证 .csv 等常见任务),以及一组单独的规则可能在 Table 模型类中的特定表?

And what if you have a combination on a form where you need to use both a Reusable Validator (for a common task like validating a .csv), and also a separate set of rules for a specific table that might be in a Table model class?

推荐答案

表提供合同

其实没那么复杂.为方便起见,在表上定义了与表相关的验证规则.它们不必在那里定义,它们可以在自定义验证类中定义,但最终表对象提供请求的验证规则集.

Tables supply the contract

It's actually not that complicated. Validation rules that concern tables are defined on tables for convenience. They don't have to be defined there, they can be defined in custom validation classes, but finally the table object supplies the requested validation rules set.

验证实体是通用验证流程的一部分.由于实体是传递给表的数据集,因此应由表保存决定实体是否有效的规则,因为这是表的问题.

Validating entities is part of the common validation flow. Since entities are sets of data that are passed to tables, it's the tables that should hold the rules that decide whether an entity is valid or not, because that's the tables concern.

另见

  • 食谱> 数据库访问 &ORM > 保存数据 > 将请求数据转换为实体

组合验证器非常简单,只需将自定义验证对象传递给表类中的 validation*() 方法,该方法提供您想要继承的规则,大致如下:

Combining validators is pretty easy, simply pass your custom validation object to the validation*() method in your table class that provides the rules that you want to inherit, something along the lines of:

public function validationDefault(Validator $validator)
{
    $validator
        ->add(/* ... */);

    return $validator;
}

public function validationCustomAndDefault()
{
    $validator = new AppModelValidationCustomModelValidator();

    return $this->validationDefault($validator);
}

然后只需将 validate 选项配置为 customAndDefault,并且您的数据/实体将使用您的自定义验证对象规则和默认规则进行验证.

Then just configure the validate option as customAndDefault, and your data/entities are being validated with your custom validation object rules, and the default ones.

另见

  • Cookbook > 数据库访问 &ORM > 验证 > 组合验证器

除此之外,验证不依赖于模式层,它只是利用它,你可以随时随地使用验证对象,即如果你想手动验证数据,只需实例化一个验证器类并使用它验证您的数据:

Besides that, validation is not tied to the mode layer, it just makes use of it, you can always use validation objects wherever you want, ie if you want to validate data manually, just instantiate a validator class and use it to validate your data:

$validator = new AppValidationCustomGenericValidator();
$errors = $validator->errors($data);

另见

  • Cookbook > 数据库访问 &ORM > 验证 > 验证数据

相关文章