問題描述
在 CakePHP 3 文檔中有一個(gè)關(guān)于可重用驗(yàn)證器的部分: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
它沒有說明你如何使用它們,但在控制器中.誰能舉個(gè)例子?
It doesn't say how you use them, in a Controller though. Can anyone give an example?
我有一個(gè)允許上傳 .csv 文件的特定應(yīng)用程序.應(yīng)用程序中 .csv 文件的驗(yàn)證始終相同:檢查其 MIME 類型、檢查大小、檢查擴(kuò)展名等.
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.
所以我的計(jì)劃是將其實(shí)現(xiàn)為可重復(fù)使用的驗(yàn)證器 - 對嗎?
So my plan was to implement this as a reusable validator - is that correct?
我有一個(gè)帶有 upload()
函數(shù)的 UploadsController.php
,我想用它來驗(yàn)證來自表單的數(shù)據(jù).我很困惑,因?yàn)榇藭r(shí)我沒有創(chuàng)建實(shí)體 - 而只是嘗試驗(yàn)證我的文件 - 所以文檔中的所有這些 patchEntity()
內(nèi)容在這里沒有任何意義.
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.
我發(fā)現(xiàn)有關(guān) Cake 3 驗(yàn)證的文檔非常混亂,因?yàn)?ORM 下有一個(gè)部分 (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
驗(yàn)證規(guī)則在Table類
但后來,它有一個(gè)完全不同的部分來驗(yàn)證實(shí)體(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).
然后我們有可重用的驗(yàn)證器......以及其他各種東西.
Then we have Reusable Validators..... and various other things.
由于 Cake 3 中的 Table 和 Entity 模型類不同,有人能解釋一下您將如何驗(yàn)證文件上傳之類的東西,特別是考慮到甚至可能根本沒有涉及表?
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?
如果您在表單上組合使用兩者可重用驗(yàn)證器(用于驗(yàn)證 .csv 等常見任務(wù)),以及一組單獨(dú)的規(guī)則可能在 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?
推薦答案
表提供合同
其實(shí)沒那么復(fù)雜.為方便起見,在表上定義了與表相關(guān)的驗(yàn)證規(guī)則.它們不必在那里定義,它們可以在自定義驗(yàn)證類中定義,但最終表對象提供請求的驗(yàn)證規(guī)則集.
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.
驗(yàn)證實(shí)體是通用驗(yàn)證流程的一部分.由于實(shí)體是傳遞給表的數(shù)據(jù)集,因此應(yīng)由表保存決定實(shí)體是否有效的規(guī)則,因?yàn)檫@是表的問題.
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.
另見
- 食譜> 數(shù)據(jù)庫訪問 &ORM > 保存數(shù)據(jù) > 將請求數(shù)據(jù)轉(zhuǎn)換為實(shí)體
組合驗(yàn)證器非常簡單,只需將自定義驗(yàn)證對象傳遞給表類中的 validation*()
方法,該方法提供您想要繼承的規(guī)則,大致如下:
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
選項(xiàng)配置為 customAndDefault
,并且您的數(shù)據(jù)/實(shí)體將使用您的自定義驗(yàn)證對象規(guī)則和默認(rèn)規(guī)則進(jìn)行驗(yàn)證.
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 > 數(shù)據(jù)庫訪問 &ORM > 驗(yàn)證 > 組合驗(yàn)證器
除此之外,驗(yàn)證不依賴于模式層,它只是利用它,你可以隨時(shí)隨地使用驗(yàn)證對象,即如果你想手動(dòng)驗(yàn)證數(shù)據(jù),只需實(shí)例化一個(gè)驗(yàn)證器類并使用它驗(yàn)證您的數(shù)據(jù):
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 > 數(shù)據(jù)庫訪問 &ORM > 驗(yàn)證 > 驗(yàn)證數(shù)據(jù)
這篇關(guān)于CakePHP 3 - 使用可重用的驗(yàn)證器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!