±¾ÎÄʵÀý½²ÊoÁËYiiºËÐÄÑeÖ¤Æ÷api¡£*ÖÏi¸ø´o¼Ò¹(C)´o¼Ò²Î¿¼£¬¾ßÌaÈçÏ£º
ºËÐÄÑeÖ¤Æ÷ Core Validators
Ç°ÑÔ
Yii Ìa¹(C)ÁËһϵÁг£ÓõĺËÐÄ validators, Äa¿ÉÔÚ
yii\validators namespace ÖÐÕÒµ½. ²»ÓÃʹÓó¤µÄÑeÖ¤Æ÷ÀaÃu, Äa¿ÉÒÔʹÓñðÃu´uÌaeËuÃÇ.
ÀýÈç, Äa¿ÉÒÔʹÓñðÃu required ´uÌae yii\validators\RequiredValidator Àa:
<?php
public function rules()
{
return [
[['email', 'password'], 'required'],
];
}
?>
Õa¸o yii\validators\Validator::$builtInValidators ÊoÐÔÉuÃuËuÓÐÖ§³ÖµÄÑeÖ¤Æ÷µÄ±ðÃu.
½ÓÏÂÀ´, ÎÒÃǽ«½²½aÿÖÖºËÐÄÑeÖ¤Æ÷µÄÓÃ*¨.
1. boolean ¶Ô´iÖµ
<?php
[
// ¼i²eÊÇ*ñ "selected" ÊÇ 0 »o 1, ²»¹ÜÊý¾ÝÀaÐÍ
['selected', 'boolean'],
// ¼i²eÊÇ*ñ "deleted" ÊÇÒ»¸o boolean ÀaÐÍ, Ö»ÄÜÊÇ true »oÔo false
['deleted', 'boolean', 'trueValue' => true, 'falseValue' => false, 'strict' => true],
]
?>
ÉÏÃae¼i²aÊÇ*ñÊÇÒ»¸oboolÖµ.
trueValue: ´u±itrueʱµÄÖµ. ĬÈÏΪ '1'.
falseValue: ´u±i false ʱµÄÖµ. ĬÈÏΪ '0'.
strict: ÊÇ*ñÊaÈe¿oÖеÄÖµ±ØÐeÆ¥Åa ÉeÖÃµÄ trueValue ºÍ falseValue. ĬÈÏΪ false.
×¢Òa: ÒoΪÊý¾ÝÌa½»Í¨¹ýHTMLÐÎʽ¶¼ÊÇ×Ö*u´´, Ò»°aÄaÐeÒªÉeÖà strict ÊoÐÔΪ false.
2. captcha ÑeÖ¤Âe
<?php
[
['verificationCode', 'captcha'],
]
?>
Õa¸oÑeÖ¤Æ÷ͨ³£ºÍ
yii\captcha\CaptchaAction ÒÔ¼°
yii\captcha\Captcha Ò»ÆðʹÓÃÒÔȱ£ÊaÈeµÄÊÇÏaͬµÄÑeÖ¤Âe ͨ¹ý CAPTCHA ×e¼þ.
caseSensitive: ÊÇñÑeÖ¤ÂeÐeÒªÇøÖ´oСд. ĬÈÏΪ false.
captchaAction: ĬÈϵÄÑeÖ¤ÂeµÄ CAPTCHA action Éu³É CAPTCHA imageµÄ¾¶. ĬÈÏΪ 'site/captcha'.
skipOnEmpty: ÊaÈeΪ¿ÕµÄʱºoÊÇ*ñÌø¹ýÑeÖ¤. ĬÈÏfalse, ¼´ input ÊaÈeÊDZØÐeµÄ.
3. compare ±È½Ï
<?php
[
// ÑeÖ¤ÊÇ*ñ "password" ÊoÐÔºÍ "password_repeat"ÊÇ*ñÏaµÈ
['password', 'compare'],
// ÑeÖ¤ age >= 30
['age', 'compare', 'compareValue' => 30, 'operator' => '>='],
]
?>
Õa¸oÑeÖ¤Æ÷±È½ÏÖ¸¶¨µÄÊaÈeÖµºÍÁiÒ»¸oֵȱ£ËuÃÇÖ®¼aʹÓÃÖ¸¶¨µÄ²Ù×÷u.
compareAttribute: ÊoÐÔµÄÃu×Ö¼´½«»aºÍ˱ȽÏ.µ±ÑeÖ¤Æ÷±»ÓÃÀ´ÑeÖ¤Ò»¸oÊoÐÔµÄʱºo, Ëu»aºÍÊoÐÔÃu×ÖÏaͬ µ«´øÓкo׺ _repeatµÄÊoÐÔÏa±È½Ï. ÀýÈç, Èç¹uÒªÑeÖ¤µÄÊoÐÔÃuÊÇ password, ÄÇôĬÈ϶ԱȵÄÊoÐÔÃu¾ÍÊÇ password_repeat Äa¿ÉÒÔÖ¸¶¨ÄaÏeÒªµÄÖµ.
compareValue: Ò»¸oºÍÊaÈeÖµ±È½ÏµÄ³£Á¿Öµ. µ± property ºÍ compareAttribute ÊoÐÔ±»Í¬Ê±Ö¸¶¨µÄʱºo, Õa¸oÖµ½«ÓÅÏÈ.
operator: ±È½ÏµÄ²Ù×÷u. ĬÈÏÊÇ ==, ¼´ÊaÈeÖµºÍ compareAttribute »o compareValue ÖµÏaµÈµÄʱºo²Å»a³É¹¦.
Ö§³ÖÏÂÃaeÕaÐ(C)²Ù×÷u: ==¡¢===¡¢!=¡¢!==¡¢>¡¢>=¡¢<¡¢<=¡£
4. date ÈÕÆÚ
<?php
[
[['from_date', 'to_date'], 'date'],
]
?>
Õa¸oÑeÖ¤Æ÷¼i²eÊaÈeµÄÖµÊÇÒ»¸oÈÕÆÚdate, ʱ¼atime »oÕßdatetime ÒÔÒ»¸oºÏÊʵÄÐÎʽ. ¿ÉÑ¡µÄ,ËuÄÜת»»ÊaÈeÖµ³ÉΪһ¸oUNIXʱ¼a´Á´ae´¢ÔÚÒ»¸oͨ¹ýtimestampAttribute Ö¸¶¨µÄÊoÐÔÀi.
format: Õa¸o date/time ÊÇ http://php.net/manual/en/datetime.createfromformat.php ÖÐÉuÃ÷µÄËuÓÐÖ§³ÖµÄ¸ñʽ
Èç¹uÕa¸oֵûÓÐÉeÖÃ, Ëu»a×Ô¶¯È¡Öµ Yii::$app->formatter->dateFormat.
timestampAttribute: ÊoÐÔÃuÓÃÀ´´ae´¢×ª»»½«ÊaÈeµÄÈÕÆÚ»oʱ¼a. Ò²¿ÉÒÔÊÇÏaͬµÄÊoÐÔÃu×÷Ϊ±»ÑeÖ¤¹ýµÄ. Èç¹uÊÇÕaÖÖÇe¿o, ÔʼµÄÖµ½«»a±»Ê±¼a´ÁÖØд. Äa¿ÉÒÔ¿´¿´ "ÈçºÎ´¦ÀiÈÕÆÚÑ¡ÔñÆ÷ DatePicker" ÓÃÀý×Ó.
http://www.yiiframework.com/doc-2.0/widget-jui#datepicker-date-input
Èç¹uÊaÈeµÄÊÇ¿ÉÑ¡µÄ£¬ÄaÒ²¿ÉÄÜÒªÌi¼ÓÒ»¸oĬÈÏÖµÂ˲¨Æ÷³ýÁËÈÕÆÚÑeÖ¤£¬ÒÔȱ£¿ÕÊaÈe´ae´¢ÎªNULL¡£ñÔoÄa¿ÉÄÜ×iÖյõ½µÄÈÕÆÚÏñÈÕÆÚÑ¡ÔñÆ÷µÄÊaÈe×Ö¶ÎÔÚ0000-00-00 »o ÄuµÄÊý¾Ý¿a1970-01-01.
<?php
[
[['from_date', 'to_date'], 'default', 'value' => null],
[['from_date', 'to_date'], 'date'],
],
?>
5.default ĬÈÏÖµ
<?php
[
// ÉeÖà "age" Ϊ null Èç¹uËuÊÇ¿ÕµÄ
['age', 'default', 'value' => null],
// ÉeÖà "country" Ϊ "USA" Èç¹uËuÊÇ¿ÕµÄ
['country', 'default', 'value' => 'USA'],
// *ÖÅa "from" ºÍ "to" *Ö±ð¼Ó3ÌiºÍ6Ìi, Èç¹uËuÃÇÊǿյĻ°
[['from', 'to'], 'default', 'value' => function ($model, $attribute) {
return date('Y-m-d', strtotime($attribute === 'to' ? '+3 days' : '+6 days'));
}],
]
?>
Õa¸oÑeÖ¤Æ÷²»ÑeÖ¤Êý¾Ý. µ±ËuÃÇΪ¿ÕµÄʱºo.*ÖÅa¸øÊoÐÔÖµ¡£
value: ±iµ¥µÄĬÈÏÖµ º¯ÊýÔÐÍÈçÏÂ,
<?php
function foo($model, $attribute) {
// ... ×e×° $value ...
return $value;
}
?>
6. double ¸¡µaÖµ
<?php
[
// ¼i²e "salary" ÊÇÒ»¸o double Êý×Ö
['salary', 'double'],
]
?>
Ïaµ±ÓÚÒ»¸oÊý×ÖÑeÖ¤Æ÷£º
max: Êý×ÖÉÏÏÞ£¬Ã»ÉeÖò»¼i²e
min: Êý×ÖÏÂÏߣ¬Ã»ÉeÖò»¼i²e
7. email ÓÊÏaÑeÖ¤
<?php
[
// ¼i²eÓÊÏaÊÇÒ»¸oÓÐЧµÄ "email"µØÖ*
['email', 'email'],
]
?>
allowName: ¼i²eÃu×ÖÊÇñÔÊÐiÔÚÓÊÏaÖгoÏÖ (e.g. John Smith john.smith@example.com). ĬÈÏΪ false.
checkDNS£¬ÊÇñ¼i²eµç×ÓÓʼþµÄÓoÃuÊÇñ´aeÔÚ£¬ÊÇñÓÐÈκÎÒ»¸oA»oMX¼Ç¼¡£Çe×¢Òa£¬Õa¿ÉÄܼi²eÓÉÓÚÁÙʱDNSÎÊÌaʧ°Ü£¬¼´Ê¹µç×ÓÓʼþµØÖʵ¼ÊÉÏÊÇÓÐЧµÄ¡£Ä¬ÈÏΪfalse¡£
enableIDN£¬ÑeÖ¤¹ý³ÌÊÇñÓ¦¿¼Âǵ½IDN£¨¹u¼Ê»¯ÓoÃu£(C)¡£Ä¬ÈÏΪfalse¡£ÐeҪעÒaµÄÊÇ£¬ÎªÁËʹÓÃIDNÑeÖ¤Äu±ØÐe°²×°²¢ÆoÓùu¼ÊPHPÀ(C)Õ¹£¬»oÒi³£½«±»Å׳o¡£
*8. exist ÊÇñ´aeÔÚ**
<?php
[
// a1 ÐeÒªÔÚ ÊoÐÔÁÐÖдaeÔÚ "a1" Õa¸oºÃÏñĬÈϾÍÓа¡
['a1', 'exist'],
// a1 ´aeÔÚ,µ«ËuµÄÖµ½«Ê¹ÓÃA2À´¼i²eÊÇ*ñ´aeÔÚ
['a1', 'exist', 'targetAttribute' => 'a2'],
// a1 ºÍ a2 ÐeҪͬʱ´aeÔÚ, ËuÃǶ¼½«½ÓÊÕ´iÎoÐÅÏ¢
[['a1', 'a2'], 'exist', 'targetAttribute' => ['a1', 'a2']],
// a1 ºÍ a2 ÐeҪͬʱ´aeÔÚ, Ö»ÓÐ a1 ½«½ÓÊÕ´iÎoÐÅÏ¢
['a1', 'exist', 'targetAttribute' => ['a1', 'a2']],
// a1 ÐeÒª´aeÔÚ Í¨¹ý¼i²aa2 ºÍ a3 (Óà a1 µÄÖµ)
['a1', 'exist', 'targetAttribute' => ['a2', 'a1' => 'a3']],
// a1 ÐeÒª´aeÔÚ. Èç¹u a1ÊÇÒ»¸oÊý×e, ÄÇôÿ¸oÔªËض¼±ØÐe´aeÔÚ.
['a1', 'exist', 'allowArray' => true],
]
?>
Õa¸oÑeÖ¤Æ÷¼i²eÊaÈeµÄÖµÄÜ*ñ±»ÕÒµ½ÔÚÔÚ¶ÔÓ¦µÄ±iµÄÁÐÖµÀi£¬ËuÖ»»aÔÚ Active Record model Ä£Ð͵ÄÊoÐÔÀiÆð×÷ÓÃ.
ËuÖ§³ÖÑeÖ¤µ¥Áлo¶aÁÐ
targetClass: ÓÃÀ´Ñ°ÕÒÊaÈeÖµÑeÖ¤µÄ Active Record ÀaÃu. Èç¹uδÉeÖÃ, ĬÈÏʹÓõ±Ç°ÉeÖõÄÄ£ÐÍÀa.
targetAttribute: Ó¦¸ÃÓÃÓÚÑeÖ¤ÊaÈeÖµµÄ´aeÔÚÔÚtargetClassµÄÊoÐÔµÄÃu³Æ¡£Èç¹uûÓÐÉeÖ㬽«Ê¹ÓÃÄ¿Ç°ÕýÔÚÑeÖ¤µÄÊoÐÔµÄÃu³Æ¡£¿ÉÒÔʹÓÃÕoÁÐÀ´ÑeÖ¤¶aÁеĴaeÔÚµÄͬʱ¡£Êý×eµÄÖµÊǽ«±»ÓÃÓÚÑeÖ¤´aeÔÚµÄÊoÐÔ£¬¶øÊý×e¼uÊÇÆaÖµÒªÑeÖ¤µÄÊoÐÔ¡£Èç¹u¼uºÍÖµ¶¼ÊÇÒ»ÑuµÄ£¬Äa¿ÉÒÔÖ¸¶¨Öµ.
filter: additional filter to be applied to the DB query used to check the existence of the input value. This can be a string or an array representing the additional query condition (refer to yii\db\Query::where() on the format of query condition), or an anonymous function with the signature function ($query), where $query is the Query object that you can modify in the function.
allowArray: ÊÇ*ñÔÊÐiÊaÈeµÄÖµÊÇÒ»¸oarray.ĬÈÏ false. Èo¹uÊoÐÔÊÇ true ¶øÇÒÊaÈeÊÇÒ»¸o array, ÄÇôÊiÕaÀiµÄÿһ¸oÔªËض¼±ØÐe´aeÔÚÔÚÖ¸ÏoµÄÄ¿±eÁÐÀi. ×¢ÒaÕa¸oÊoÐÔ²»Äܱ»ÉeÖóÉtrue Èç¹uÄaÉeÖÃÑeÖ¤¶aÁÐͨ¹ýÉeÖà targetAttribute Ϊһ¸o array.
9. file ÎļþÑeÖ¤
<?php
[
// ¼i²e "primaryImage" ÉÏ´«µÄÎļþ¸ñʽÊÇ PNG, JPG »oÕß GIF
// the file size must be less than 1MB
['primaryImage', 'file', 'extensions' => ['png', 'jpg', 'gif'], 'maxSize' => 1024*1024],
]
?>
¼i²eÊÇ*ñÊÇÒ»¸oÓÐЧµÄÉÏ´«Îļþ¡£
extensions: ÔÊÐiÉÏ´«µÄÎļþÀaÐÍÁбi.Ëu¿ÉÒÔÊÇÒ»¸oÊý×e »oÕßÊÇÒ»¸oÒÔ¶ººÅÖ¸iµÄ×Öu´®(Èç. "gif, jpg"). À(C)Õ¹Ãu²»ÇøÖ´oСд£¬Ä¬ÈÏΪ null, Òaζ×ÅËuÓеÄÎļþÀ(C)Õ¹ÃuÊÇÔÊÐiµÄ.
mimeTypes: ÔÊÐiÉÏ´«µÄÎļþ×ÊÔ´µÄýÌaÀaÐÍ¡£Ëu¿ÉÒÔÊÇÒ»¸oÊý×e »oÕßÊÇÒ»¸oÒÔ¶ººÅ»o¿Õ¸ñÖ¸iµÄ×Öu´® (Èç. "image/jpeg, image/png"). ²»ÇøÖ´oСд£¬Ä¬ÈÏΪ null, Òaζ×ÅËuÓеÄÎļþÀ(C)Õ¹ÃuÊÇÔÊÐiµÄ.
minSize: ÉÏ´«ÎļþµÄ×iС×Ö½ÚÊý δÉe²»×oÅжÏ.
maxSize: ÉÏ´«ÎļþµÄ×i´o×Ö½ÚÊý δÉe²»×oÅжÏ.
maxFiles: ×i´oµÄÉÏ´«ÎļþÊý ĬÈÏΪ 1, Òaζ×ÅÖ»ÄÜÉÏ´«µ¥¸oÎļþ¶ÔÓÚµ¥¸oÎļþÉÏ´«¿o. Èç¹u´oÓÚ 1, ÄÇô input±ØÐeÊÇÒ»¸o array °uº¬ÖÁ¶a maxFiles ÊýÁ¿µÄÉÏ´«Îļþ.
checkExtensionByMimeType: ÊÇ*ñ¼i²eÎļþµÄÀ(C)Õ¹Ãu. Èç¹uÓÉMIMEÀaÐͼi²eËu²uÉuµÄÀ(C)Õ¹²»Í¬ÓÚÉÏ´«µÄÎļþÀ(C)Õ¹Ãu£¬¸ÃÎļþ½«±»ÈÏΪÊÇÎÞЧµÄ¡£Ä¬ÈÏֵΪtrue£¬Òa˼½øÐÐÕaÑuµÄ¼i²e¡£
FileValidator ºÍ yii\web\UploadedFile Ò»ÆðʹÓÃ. .
ÎļþÉÏ´«Çe²Î¿¼£ºhttp://www.yiiframework.com/doc-2.0/guide-input-file-upload.html
10. filter ¹ýÂË
<?php
[
// È¥µo "username" ºÍ "email" ÊaÈe¿Õ¸ñ Ìø¹ýÊý×eÑeÖ¤
[['username', 'email'], 'filter', 'filter' => 'trim', 'skipOnArray' => true],
// ÑeÖ¤Õý³£ "phone" ÊaÈe
['phone', 'filter', 'filter' => function ($value) {
// ÑeÖ¤ÕýÈ*ÐÔÊÖ»u´uÂe
return $value;
}],
]
?>
Õa¸oÑeÖ¤Æ÷ÑeÖ¤²»ÁËÊý¾Ý. Ïa*´µÄ, it applies a filter on the input value and assigns it back to the attribute being validated.
filter: Ò»¸ophp»Øµ÷º¯Êý¶¨ÒaÒ»¸ofilter. Ëu¿ÉÄÜÊÇÒ»¸oÈ«¾Öº¯ÊýµÄÃu×Ö, Ò»¸oÄaÃuº¯ÊýµÈµÈ. º¯ÊýµÄÔÐÍÒ»¶¨ÊÇ<?php function ($value) { return $newValue; } ?>Õa¸oÊoÐÔ±ØÐeÉeÖÃ.
skipOnArray: µ±ÊaÈeÖ»ÊÇÒ»¸oÊý×eµÄʱºoÊÇñÌø¹ý.ĬÈÏÊÇ false. Çe×¢Òa£¬Èç¹u¹ýÂËÆ÷ÎÞ¨´¦ÀiÊý×eÊaÈe£¬ÄuÓ¦¸Ã½«´ËÊoÐÔÉeÖÃΪtrue¡£ñÔo£¬¿ÉÄÜ»a¢ÉuÒ»Ð(C)PHP´iÎo.
СÌaʾ: È¥¿Õ¸ñÖ±½ÓÓÃtrimÑeÖ¤.
СÌaʾ: ²¿Öa×°¿ÉÒÔÖ±½ÓʹÓÃϵͳº¯ÊýÈçintval:
<?php
['property', 'filter', 'filter' => 'boolval'],
['property', 'filter', 'filter' => 'intval'],
?>
11. image ͼƬ
<?php
[
// ¼i²e "primaryImage" ÊÇ*ñÊÇÒ»¸oÓÐЧµÄͼƬ ÇÒ´oСºÏÊÊ
['primaryImage', 'image', 'extensions' => 'png, jpg',
'minWidth' => 100, 'maxWidth' => 1000,
'minHeight' => 100, 'maxHeight' => 1000,
],
]
?>
ÎļþÑeÖ¤Æ÷µÄÀ(C)Õ¹:
minWidth: ×iСµÄ¿i¶È ²»ÉeÎÞÏÞÖÆ.
maxWidth: ×i´oµÄ¿i¶È ²»ÉeÎÞÏÞÖÆ.
minHeight: ×iСµÄ¸ß¶È ²»ÉeÎÞÏÞÖÆ.
maxHeight: ×i´oµÄ¸ß¶È ²»ÉeÎÞÏÞÖÆ.
12. in ÔÚÄÇÒ»¸oÊý×eÀiÃaeµÄÖµ
<?php
[
// ¼i²e "level" ÊÇ 1, 2 »o 3
['level', 'in', 'range' => [1, 2, 3]],
]
?>
¼i²eÊaÈeµÄÖµÊÇ*ñ´aeÔÚÓÚ¸ø¶¨µÄϵÁÐÖµµÄÁбiÖÐ.
range: ¸ø¶¨¶Î§ÖµµÄÁбi.
strict: ÊaÈeÖµºÍ¸ø¶¨ÖµÖ®¼aµÄ±È½Ï½a¹uÊÇñÓ¦Ñϸñ£¨µÄÀaÐͺÍÖµ±ØÐeÏaͬ£(C)¡£Ä¬ÈÏΪfalse¡£
not: ÑeÖ¤½a¹uÊÇñÓ¦¸Ã±»´×ª¡£Ä¬ÈÏΪfalse¡£µ±´ËÊoÐÔÉeÖÃΪtrue£¬ÑeÖ¤¼i²e£¬ÊaÈeµÄÖµ²»ÄÜÊǸø¶¨ÁбiµÄÖµÖÐ.
allowArray: ÊÇ*ñÔÊÐiÊaÈeÖµÊÇÒ»¸oÊý×e¡£Èç¹uÕaÊÇÕaeµÄ£¬²¢ÊaÈeÖµÊÇÒ»¸oÊý×e£¬Êý×eÖеÄÿ¸oÔªËرØÐeÖµµÄ¸ø¶¨ÁбiÖÐÕÒµ½£¬»oÑeÖ¤»aʧ°Ü¡£
13. integer ÕuÐÍÑeÖ¤
<?php
[
// ¼i²e "age" ÊÇÒ»¸oÕuÐÍÖµ
['age', 'integer'],
]
?>
max: ÉÏÏÞ ²»Ée²»¼i²e.
min: ÏÂÏÞ ²»Ée²»¼i²e..
14. match ÕýÔoÆ¥Åa¼i²e
<?php
[
// ¼i²e "username" ÒÔ×Öĸ¿ªÍ*£¬²¢ÇÒÖ»°uº¬ÎÄ×Ö×Ö*u
['username', 'match', 'pattern' => '/^[a-z]\w*$/i']
]
?>
ÊaÈeÖµÖ¸¶¨µÄÕýÔo±i´iʽƥÅaÑeÖ¤¼i²e.
pattern: ÊaÈeÖµ±ØÐeÆ¥ÅaµÄ¹aeÔo. ÊoÐÔ±ØÐeÉeÖÃ,ñÔo½«»aÅ׳oÒi³£.
not: ÊÇñ*´×ªÑeÖ¤½a¹u. ĬÈÏ false.
15. number Êý×Ö¼i²e
<?php
[
// ¼i²e "salary" ÊÇÒ»¸oÊý×Ö
['salary', 'number'],
]
?>
Ïaµ±ÓÚdoubleÑeÖ¤Æ÷.
max: ÉÏÏÞ ²»Ée²»¼i²e.
min: ÏÂÏÞ ²»Ée²»¼i²e.
*16. required ÊÇñΪ¿Õ¼i²e**
<?php
[
// ¼i²e "username" ºÍ "password" ²»ÊÇ¿ÕµÄ
[['username', 'password'], 'required'],
]
?>
¸ÃÑeÖ¤Æ÷×eÖ¹Óû§Ìa½»¿ÕµÄ±iµ¥Êý¾Ý.
requiredValue: Ó¦¸ÃÊaÈeµÄÖµ. Èç¹uδÉe Òaζ×ÅÊaÈeÖµ ²»Ó¦¸ÃΪ¿Õ.
strict: ÑeÖ¤Ò»¸oֵʱÊÇñÓ¦¸Ã¼i²eÊý¾ÝÀaÐÍ. ĬÈÏfalse.µ± requiredValueδÉeÖÃʱ,Èç¹uÕa¸oÊoÐÔÊÇ true, ÑeÖ¤Æ÷»a¼i²eÊaÈeÖµ²»Ñϸñnull;Èç¹u¸ÄÊoÐÔfalse,ÑeÖ¤Æ÷½«Ê¹ÓÃÒ»¸oËÉÉ¢µÄ¹aeÔoÀ´È¶¨Ò»¸oÖµÊǿյĻo²». µ± requiredValueÉeÖÃÁË,ÊaÈeºÍrequiredValueÖ®¼aµÄ±È½ÏÒ²½«¼i²eÊý¾ÝÀaÐÍ£¬Èç¹u´ËÊoÐÔΪtrue¡£
17. safe ±e¼ÇÊaÈeÊÇ°²È«ÊoÐÔ
<?php
[
// ±e¼Ç "description" ÊÇÒ»¸o safe µÄÊoÐÔ
['description', 'safe'],
]
?>
Õa¸oÑeÖ¤Æ÷²»»aÖ´ÐÐÊý¾ÝÑeÖ¤.Ïa´, ËuÊÇÓÃÀ´±e¼ÇµÄÊoÐÔÊÇÒ»¸o°²È«ÊoÐÔ(ÎÒ²Â×÷ÓÃÊDz»»a½øÐÐ×ÖuתÒa°É)¡£
*18. string ×Öu´®ÑeÖ¤**
<?php
[
// ¼i²e "username"ÊÇÒ»¸o string ¶øÇÒ³¤¶ÈÔÚ 4 ºÍ 24 Ö®¼a
['username', 'string', 'length' => [4, 24]],
]
?>
Õa¸oÑeÖ¤Æ÷¼i²eÊaÈeµÄÖµÊÇÒ»¸o×Öu´®ÇÒ³¤¶ÈÔÚȶ¨µÄÖµµÄ*¶Î§Ài¡£
length: Ö¸¶¨´ýÑeÖ¤µÄ×Öu´®µÄ³¤¶È. ¿ÉÒÔÖ¸¶¨ÒÔÏÂÐÎʽ:
Ò»¸oÕuÊý: ×Öu´®µÄ¾«È³¤¶È;
µ¥¸oÊý×eÔªËØ: ÊaÈeµÄ×iС³¤¶È (e.g. [8]). ±ØÐe³¬¹ý»oµÈÓÚÕa¸oÊý
Á½¸oÊý×eÔªËØ: ÊaÈeµÄ×iС³¤¶ÈºÍ×i´o³¤¶È (e.g. [8, 128]).
min: ÊaÈe×Öu´®µÄ×iС³¤¶È δÉe²»ÏÞÖÆ.
max: ÊaÈe×Öu´®µÄ×i´o³¤¶È δÉe²»ÏÞÖÆ..
encoding: ÊaÈe×Öu´®µÄ±aÂe δÉeĬÈÏ UTF-8.
19. trim È¥¿Õ¸ñ
<?php
[
// È¥µo "username" ºÍ "email" Á½±ßµÄ¿Õ¸ñ
[['username', 'email'], 'trim'],
]
?>
²»½øÐÐÊý¾ÝÑeÖ¤ Ö»½øÐÐÈ¥¿Õ¸ñ Èç¹uÊoÐÔÊÇÒ»¸oÊý×e ½«»a×Ô¶¯ºoÂԴ˹ýÂË.
20. unique ΨһÐÔÑeÖ¤
<?php
[
// a1 ÐeÒªÊÇÔÚÓÉ¡°a1¡±ÊoÐÔ±iʾµÄÁÐÖеÄΨһ
['a1', 'unique'],
// a1 ±ØÐeÊÇΨһµÄ£¬µ«ÁÐa2½«ÓÃÓÚ¼i²eµÄa1ÖµµÄΨһÐÔ
['a1', 'unique', 'targetAttribute' => 'a2'],
// a1ºÍa2µÄÐeÒªÊÇΨһµÄ£¬²¢ÇÒËuÃǶ¼½«ÊÕµ½´iÎoÏuÏ¢
[['a1', 'a2'], 'unique', 'targetAttribute' => ['a1', 'a2']],
// a1ºÍa2µÄÐeÒªÊÇΨһµÄ£¬Ö»ÓÐa1½«ÊÕµ½´iÎoÏuÏ¢
['a1', 'unique', 'targetAttribute' => ['a1', 'a2']],
// a1ÐeҪͨ¹ý¼i²eµÄÁ½¸oa2ºÍa3µÄΨһÐÔ£¨Ê¹ÓÃa1µÄÖµ£(C)ÊÇΨһµÄ
['a1', 'unique', 'targetAttribute' => ['a2', 'a1' => 'a3']],
]
?>
ÕaÑeÖ¤¼i²e£¬Èç¹uÊaÈeµÄÖµÊDZiÁÐÖеÄΨһ¡£Ëu½oÊÊÓÃÓÚ»i¶¯¼Ç¼ģÐ͵ÄÊoÐÔ¡£ËuÖ§³ÖÑeÖ¤¶ÔÈκε¥¸oÁлo¶a¸oÁС£
targetClass: Ó¦¸ÃÊDZ»ÓÃÓÚÑeÖ¤²eÕÒÊaÈeÖµµÄ»i¶¯¼Ç¼ÀaµÄÃu×Ö¡£Èç¹uûÓÐÉeÖ㬽«ÒªÊ¹ÓõÄÀaÄ¿Ç°ÕýÔÚÑeÖ¤¸ÃÄ£Ð͵Ä.
targetAttribute: Ó¦¸ÃÓÃÀ´ÑeÖ¤ÊaÈeÖµµÄΨһÐÔÔÚtargetClassµÄÊoÐÔµÄÃu³Æ¡£Èç¹uûÓÐÉeÖ㬽«Ê¹ÓÃÄ¿Ç°ÕýÔÚÑeÖ¤µÄÊoÐÔµÄÃu³Æ¡£¿ÉÒÔʹÓÃÕoÁÐÀ´ÑeÖ¤¶aÁеÄΨһÐÔµÄͬʱ¡£Êý×eµÄÖµÊǽ«±»ÓÃÓÚÑe֤ΨһÐÔµÄÊoÐÔ£¬¶øÊý×e¼uÊÇÆaÖµÒªÑeÖ¤µÄÊoÐÔ¡£Èç¹u¼uºÍÖµ¶¼ÊÇÒ»ÑuµÄ£¬Äa¿ÉÒÔÖ¸¶¨µÄÖµ¡£
filter: a¶iÍaµÄ¹ýÂËÆ÷±»Ó¦Óõ½ÓÃÓÚ¼i²eÊaÈeÖµµÄΨһÐÔµÄÊý¾Ý¿a²eѯ¡£Õa¿ÉÒÔÊÇÒ»¸o×Ö*u´®»o´u±i¸½¼Ó²eѯÌo¼þµÄÊý×e(²Î¿¼ yii\db\Query::where() ²eѯÌo¼þµÄ¸ñʽ), »oÕßÊÇÒ»¸oÄaÃuº¯ÊýÏñ£º function ($query), ÆaÖÐ$queryÊÇÄa¿ÉÒÔÔÚº¯ÊýÐ޸IJeѯ¶ÔÏo¡£
http://www.yiiframework.com/doc-2.0/yii-db-query.html#where()-detail
*21. url µØÖÑeÖ¤**
<?php
[
// ¼i²e "website" ÊÇÒ»¸oÓÐЧµÄ URL. Ç°Öà "http://" µ½ "website" ÊoÐÔ
// Èç¹uËu²»¾ßÓÐÒ»¸oURIģʽ
['website', 'url', 'defaultScheme' => 'http'],
]
?>
validSchemes: Ò»¸oÊý×eÖ¸¶¨URI½°¸Ó¦±»ÈÏΪÓÐЧ¡£Ä¬ÈÏΪ['HTTP'£¬'HTTPS']£¬Òa˼ÊÇHTTPºÍHTTPS URL±»ÈÏΪÊÇÓÐЧµÄ¡£
defaultScheme: ĬÈÏURI½°¸ÒªÔ¤ÏÈ¿¼Âǵ½ÊaÈe£¬Èç¹uËu²»¾ßÓн°¸µÄÒ»²¿Ö¡£Ä¬ÈÏΪnull£¬Òaζ×Ų»ÐÞ¸ÄÊaÈeÖµ¡£
enableIDN£ºÑeÖ¤ÊÇñÓ¦¿¼Âǵ½IDN£¨¹u¼Ê»¯ÓoÃu£(C)¡£Ä¬ÈÏΪfalse¡£ÐeҪעÒaµÄÊÇ£¬ÎªÁËʹÓÃIDNÑeÖ¤Äu±ØÐe°²×°²¢ÆoÓùu¼ÊPHPÀ(C)Õ¹£¬ñÔoÒi³£½«»a±»Å׳o¡£
¸u¶a¹ØÓÚYiiÏa¹ØÄÚÈݸÐÐËȤµÄ¶ÁÕ߿ɲe¿´±¾Õ¾×¨Ìa£º¡¶Yii¿o¼ÜÈeÃż°³£Óü¼ÇÉ×ܽa¡¡¢¡¶[phpÓÅÐa¿ª¢¿o¼Ü×ܽa](http://www.jb51.net/Special/155.htm)¡¡¢¡¶smartyÄ£°aÈeÃÅ»u´¡½Ì³Ì¡¡¢¡¶phpÃaeÏo¶ÔÏo³ÌÐoÉe¼ÆÈeÃŽ̡̳¡¢¡¶[php×Öu´®(string)Óè×ܽa](http://www.jb51.net/Special/47.htm)¡¡¢¡¶php+mysqlÊý¾Ý¿a²Ù×÷ÈeÃŽ̡̳¼°¡¶php³£¼uÊý¾Ý¿a²Ù×÷¼¼ÇÉ»a×Ü¡
Ï£Íu±¾ÎÄËuÊo¶Ô´o¼Ò»uÓÚYii¿o¼ÜµÄPHP³ÌÐoÉe¼ÆÓÐËu°iÖu¡£