国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目錄
什麼是 CakePHP 驗(yàn)證?
CakePHP 驗(yàn)證方法
1.加
2.允許空
3.字母數(shù)字
4.信用卡
5.電子郵件
6.最大長(zhǎng)度
7. minLength
How to Create CakePHP Validation?
Conclusion
首頁(yè) 後端開發(fā) php教程 CakePHP 驗(yàn)證

CakePHP 驗(yàn)證

Aug 29, 2024 pm 12:58 PM
php

CakePHP 是一個(gè)用於實(shí)現(xiàn)動(dòng)態(tài)程式設(shè)計(jì)應(yīng)用程式的開源工具,並為開發(fā)人員提供了不同類型的功能。驗(yàn)證是 CakePHP 提供的功能之一,透過使用驗(yàn)證,我們可以根據(jù)我們的要求為任意資料數(shù)組提供驗(yàn)證。在 CakePHP 中,我們需要在資料驗(yàn)證之前根據(jù)形狀和大小建立實(shí)體。這裡我們還需要考慮預(yù)設(shè)實(shí)體,這些實(shí)體將在實(shí)體對(duì)話之前進(jìn)行驗(yàn)證。我們也可以根據(jù)我們的要求應(yīng)用驗(yàn)證規(guī)則。

開始您的免費(fèi)軟體開發(fā)課程

網(wǎng)頁(yè)開發(fā)、程式語(yǔ)言、軟體測(cè)試及其他

什麼是 CakePHP 驗(yàn)證?

資訊批準(zhǔn)是任何應(yīng)用程式的重要組成部分,因?yàn)樗兄洞_保模型中的資訊適應(yīng)應(yīng)用程式的業(yè)務(wù)規(guī)則。例如,您應(yīng)該確保密碼長(zhǎng)度約為八個(gè)字符,或保證使用者名稱是特殊的。表徵批準(zhǔn)規(guī)則使結(jié)構(gòu)處理變得更加簡(jiǎn)單。

對(duì)於審批週期有不同的看法。我們將在本節(jié)中介紹的是模型方面的內(nèi)容?;旧希寒?dāng)您為模型呼叫 save() 技術(shù)時(shí)會(huì)發(fā)生什麼。有關(guān)如何處理顯示審批錯(cuò)誤的更多資料。

CakePHP 驗(yàn)證方法

現(xiàn)在讓我們來(lái)看看CakePHP中不同的驗(yàn)證方法如下。

1.加

將另一個(gè)標(biāo)準(zhǔn)加入到欄位的標(biāo)準(zhǔn)集中。如果後續(xù)爭(zhēng)用是一個(gè)簇,則該欄位的規(guī)則清單將被第二個(gè)爭(zhēng)用所取代,而第三個(gè)爭(zhēng)用將被忽略。

文法

Add(string $specified field, array|string $specified name,
array|Cake\Validation\ValidationRule $required rule [])

說(shuō)明

在上面的語(yǔ)法中,我們使用不同參數(shù)的 add 方法。在上面的語(yǔ)法中,指定名稱用於定義我們需要新增的規(guī)則的名稱。此陣列用於根據(jù)要求定義此規(guī)則或多個(gè)規(guī)則,並傳回 $this.

2.允許空

透過使用這個(gè)方法,我們可以允許空白欄位

文法

allowEmpty(string $specified field, boolean|string|callable $whentrue, string|null msgull)

說(shuō)明

在上面的語(yǔ)法中,我們使用不同參數(shù)的 add 方法。在上面的語(yǔ)法中,指定名稱用於定義我們需要新增的規(guī)則的名稱。布林參數(shù)用於指示何時(shí)需要允許清空,這裡我們也可以在執(zhí)行建立或更新操作時(shí)根據(jù) true 或 false 進(jìn)行驗(yàn)證。訊息用於顯示訊息字段,這將返回 $this.

3.字母數(shù)字

透過使用此方法,我們可以根據(jù)我們的要求向欄位新增字母數(shù)字規(guī)則。

文法

alphanumeric (string $specified field, string|null $Msgnull, string|callable|null $whennull)

說(shuō)明

在上面的語(yǔ)法中,我們使用不同參數(shù)的字母數(shù)字方法。在上面的語(yǔ)法中,指定名稱用於定義我們需要新增的規(guī)則的名稱。將另一個(gè)標(biāo)準(zhǔn)新增至欄位的標(biāo)準(zhǔn)集中。如果後續(xù)爭(zhēng)用是一個(gè)簇,則該欄位的規(guī)則清單將被第二次爭(zhēng)用取代,第三次爭(zhēng)用將被忽略,並傳回 $this。

4.信用卡

透過該方法,我們可以根據(jù)需要在指定欄位中新增信用卡規(guī)則。

文法

creditCard(string $specified field , string $type'all', string|null $msgnull, string|callable|null $whennull)

說(shuō)明

在上面的語(yǔ)法中,我們使用信用卡的方法來(lái)新增具有不同參數(shù)的規(guī)則。您需要應(yīng)用標(biāo)準(zhǔn)的領(lǐng)域。

您需要允許的卡片類型。預(yù)設(shè)為“全部”。您還可以提供多種認(rèn)可的卡片類型,例如“mastercard”、“visa”、“amex”。

標(biāo)準(zhǔn)達(dá)不到標(biāo)準(zhǔn)時(shí)的錯(cuò)誤訊息。當(dāng)應(yīng)該應(yīng)用批準(zhǔn)規(guī)則並傳回 $this 時(shí),「make」或「update」或一個(gè)可呼叫的函數(shù)有效。

5.電子郵件

透過使用此方法,我們可以根據(jù)我們的要求向該欄位新增電子郵件驗(yàn)證規(guī)則。

文法

Email(string $specified field , boolean $checkMXfalse, string|null $msgnull, string|callable|null, $whennull)

說(shuō)明

透過使用上面的語(yǔ)法,我們可以實(shí)作電子郵件驗(yàn)證規(guī)則。您也需要應(yīng)用標(biāo)準(zhǔn)的領(lǐng)域。

無(wú)論是否檢查MX記錄。

標(biāo)準(zhǔn)失敗時(shí)的錯(cuò)誤訊息。

當(dāng)需要套用核準(zhǔn)規(guī)則時(shí),「製作」或「更新」或可呼叫的函數(shù)有效。

6.最大長(zhǎng)度

透過使用此方法,我們可以對(duì)欄位套用字串驗(yàn)證。

文法

maxLength(string $specified field, integer $max, string|null $msgnull, string|callable|null $whennull)

說(shuō)明

In the above syntax, we use the maxLength method with different parameters. Here the specified field is used to define the field to which we want to apply the rule, max is used to define the maximum length of string, msgnull is used to show an error message when the rule fails.

7. minLength

By using this method, we can apply string validation to the field.

Syntax

minLength(string $specified field, integer $min, string|null $msgnull, string|callable|null $whennull)

Explanation

In the above syntax, we use the minLength method with different parameters. Here the specified field is used to define the field which we want to apply the rule, min is used to define the minimum length of string, msgnull is used to show an error message when the rule fails.

How to Create CakePHP Validation?

Now let’s see how we can create CakePHP validation with examples as follows.?First, we need to make the changes in routes.php file as follows.

<?php
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
$routes->setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $builder) {
$builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([
'httpOnly' => true,
]));
$builder->applyMiddleware('csrf');
//$builder->connect('/pages',['controller'=>'Pages','action'=>'display', 'home']);
$builder->connect('validation',['controller'=>'Valid','action'=>'index']);
$builder->fallbacks();
});
?>

Now create an index.php file and write the following code as follows.

<?php
if($errors) {
foreach($errors as $error)
foreach($error as $mssg)
echo '<font color="red">'.$mssg.'</font><br>';
} else {
echo "There is no errors.";
}
echo $this->Form->create(NULL,array('url'=>'/validation'));
echo $this->Form->control('username of person');
echo $this->Form->control('password');
echo $this->Form->button('Submit');
echo $this->Form->end();
?>

Now execute the above code we will get the following screen as shown below screenshot.

CakePHP 驗(yàn)證

Suppose let’s consider, if we enter only password then it shows username is required as shown in the following screenshot.

CakePHP 驗(yàn)證

Similarly, we can apply validation for username of person filed as shown in the following screenshot as follows.

CakePHP 驗(yàn)證

In this way, we can implement different methods such as to get, post as per our requirement.

Conclusion

We hope from this article you learn more about the CakePHP validation. From the above article, we have taken in the essential idea of the CakePHP validation and we also see the representation and example of the CakePHP validation. From this article, we learned how and when we use the CakePHP validation.

以上是CakePHP 驗(yàn)證的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁(yè)開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)程式碼編輯軟體(SublimeText3)

我如何了解最新的PHP開發(fā)和最佳實(shí)踐? 我如何了解最新的PHP開發(fā)和最佳實(shí)踐? Jun 23, 2025 am 12:56 AM

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

什麼是PHP,為什麼它用於Web開發(fā)? 什麼是PHP,為什麼它用於Web開發(fā)? Jun 23, 2025 am 12:55 AM

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

如何設(shè)置PHP時(shí)區(qū)? 如何設(shè)置PHP時(shí)區(qū)? Jun 25, 2025 am 01:00 AM

tosetTherightTimeZoneInphp,restate_default_timezone_set()functionAtthestArtofyourscriptWithavalIdidentIdentifiersuchas'america/new_york'.1.usedate_default_default_timezone_set_set()

我如何驗(yàn)證PHP中的用戶輸入以確保其符合某些標(biāo)準(zhǔn)? 我如何驗(yàn)證PHP中的用戶輸入以確保其符合某些標(biāo)準(zhǔn)? Jun 22, 2025 am 01:00 AM

TovalidateuserinputinPHP,usebuilt-invalidationfunctionslikefilter_var()andfilter_input(),applyregularexpressionsforcustomformatssuchasusernamesorphonenumbers,checkdatatypesfornumericvalueslikeageorprice,setlengthlimitsandtrimwhitespacetopreventlayout

什麼是php(serialize(),Unserialize())中的數(shù)據(jù)序列化? 什麼是php(serialize(),Unserialize())中的數(shù)據(jù)序列化? Jun 22, 2025 am 01:03 AM

thephpfunctionserize()andunSerialize()redustoconvertComplexdatStructDestoresToroStoroStoroSandaBackagagain.1.Serialize()

如何將PHP代碼嵌入HTML文件中? 如何將PHP代碼嵌入HTML文件中? Jun 22, 2025 am 01:00 AM

可以將PHP代碼嵌入HTML文件中,但需確保文件以.php為擴(kuò)展名,以便服務(wù)器能正確解析。使用標(biāo)準(zhǔn)的標(biāo)籤包裹PHP代碼,可在HTML中任意位置插入動(dòng)態(tài)內(nèi)容。此外,可在同一文件中多次切換PHP與HTML,實(shí)現(xiàn)條件渲染等動(dòng)態(tài)功能。務(wù)必注意服務(wù)器配置及語(yǔ)法正確性,避免因短標(biāo)籤、引號(hào)錯(cuò)誤或遺漏結(jié)束標(biāo)籤導(dǎo)致問題。

編寫清潔和可維護(hù)的PHP代碼的最佳實(shí)踐是什麼? 編寫清潔和可維護(hù)的PHP代碼的最佳實(shí)踐是什麼? Jun 24, 2025 am 12:53 AM

寫乾淨(jìng)、易維護(hù)的PHP代碼關(guān)鍵在於清晰命名、遵循標(biāo)準(zhǔn)、合理結(jié)構(gòu)、善用註釋和可測(cè)試性。 1.使用明確的變量、函數(shù)和類名,如$userData和calculateTotalPrice();2.遵循PSR-12標(biāo)準(zhǔn)統(tǒng)一代碼風(fēng)格;3.按職責(zé)拆分代碼結(jié)構(gòu),使用MVC或Laravel式目錄組織;4.避免麵條式代碼,將邏輯拆分為單一職責(zé)的小函數(shù);5.在關(guān)鍵處添加註釋並撰寫接口文檔,明確參數(shù)、返回值和異常;6.提高可測(cè)試性,採(cǎi)用依賴注入、減少全局狀態(tài)和靜態(tài)方法。這些做法提升代碼質(zhì)量、協(xié)作效率和後期維護(hù)便利性。

如何使用PHP執(zhí)行SQL查詢? 如何使用PHP執(zhí)行SQL查詢? Jun 24, 2025 am 12:54 AM

Yes,youcanrunSQLqueriesusingPHP,andtheprocessinvolveschoosingadatabaseextension,connectingtothedatabase,executingqueriessafely,andclosingconnectionswhendone.Todothis,firstchoosebetweenMySQLiorPDO,withPDObeingmoreflexibleduetosupportingmultipledatabas

See all articles