久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

CakePHP 3 - 使用可重用的驗(yàn)證器

CakePHP 3 - Using reusable validators(CakePHP 3 - 使用可重用的驗(yàn)證器)
本文介紹了CakePHP 3 - 使用可重用的驗(yàn)證器的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

在 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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Session is lost after an OAuth redirect(OAuth 重定向后會(huì)話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個(gè)應(yīng)用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯(cuò)誤:允許的內(nèi)存大小為 134217728 字節(jié)已用盡(嘗試分配 87 字節(jié)))
主站蜘蛛池模板: 精品国产18久久久久久二百 | 亚洲 日本 欧美 中文幕 | 激情五月综合网 | 伦理二区| 日韩久久久久 | 天天搞天天操 | 日本三级线观看 视频 | 午夜极品 | 欧美日韩国产精品激情在线播放 | 欧美一级视频免费看 | 99久久久国产精品 | 亚洲视频自拍 | 狠狠干狠狠操 | 亚洲综合网站 | 欧美第一区 | 久久一区二区三区四区五区 | 91视频在线观看 | 久久精品国产亚洲一区二区三区 | 亚洲精品一区在线 | 欧美日韩午夜精品 | 国产精品福利久久久 | 天天操精品视频 | 国产香蕉视频在线播放 | 久久999 | 亚洲一区二区不卡在线观看 | 亚洲国产精品一区二区三区 | 日本免费视频 | 国产精品日产欧美久久久久 | 国产精品久久久久久久久久久免费看 | 欧美国产精品久久久 | 国产精品视频一 | 精品国产18久久久久久二百 | 一区在线播放 | 国产九九av | 日韩欧美国产精品一区二区三区 | 国产精品欧美一区二区三区不卡 | 中文字幕精品视频在线观看 | 欧美激情久久久 | 欧美一级片免费看 | 水蜜桃久久夜色精品一区 | 91精品久久久久久久久久入口 |