CakePHP 3-使用可重用的验证器 [英] CakePHP 3 - Using reusable validators

查看:63
本文介绍了CakePHP 3-使用可重用的验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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

虽然没有说您如何在Controller中使用它们。谁能举个例子?

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.

所以我的计划是

我有一个 UploadsController.php 和一个 upload()函数,在这里我要使用它来验证来自表单的数据。我很困惑,因为此时我还没有创建实体-而是试图验证我的文件-因此文档中的所有 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/zh-CN/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中的表和实体模型类有所不同,有人可以解释一下如何验证文件上传之类的东西,特别是考虑到甚至根本不涉及表时?

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?

如果您需要在表单上同时使用 both 可重用验证程序(用于验证的常见任务)怎么办? 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.

另请参见

组合验证器非常简单,只需将自定义验证对象传递给表类中的 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 \App\Model\Validation\CustomModelValidator();

    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.

另请参见

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

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 \App\Validation\CustomGenericValidator();
$errors = $validator->errors($data);

另请参见

这篇关于CakePHP 3-使用可重用的验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆