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XI4TaA9BVMJXQjOj3r3toi645Qd6ULtxP0vJjSstZ8kh00s42RR19Bn0I3ogfb2+TeInVXulQ7wbDS8tZ8/qgobg6l6StY8Eb0JF6+zJ63NtRd6VLtBGNKJ2bR/k4V/4jTV1BO+o3oQbPp7uy4OzvDo6Ph0dHo5GR0eUniHQGl51Za2pqva9tR2UKfafoKSnH39wdPnrg7O+7BwcTby8vR5SWpdX7qq3TZX3PnSY7HZJ0D4+dCoWaDXjjppa8gLIj6Kk3vseJUqWNRbUFplM4PSleA+ioNxUHpCoDSAEaB0gBGgdIARoHSAEaB0gBGgdIARoHSAEZRYaW/vn4WiX+/f1rEggEqjFFKL9Hq4DnnxfYDAVgw6pU+PT29v79PWQ1BEHPGSpV2HMdxnIuLi36/r/ydE4SRoUBpx3E6nY7yd04QRoYapR3HUf7OCcLIQGmCMCpQmiCMCpQmCKNCgdJnZ2e9Xq/QvP6N6OLXiI2yZF8LY0ETvKCen/vSUHcylD4+Pr67uysxr/RRE2mEX5eltNBfr9Fqxdp7ojTUnf8BYC332N6e03EAAAAASUVORK5CYII=)
<?php
//app指的是网站的根目录
//命名空间指的是文件存放的位置
namespace app\controllers;
//yii指的是网站根目录下vendor\yiisoft\yii2
use yii\web\Controller;
use app\models\Yonghu;
use app\models\Test;
class TestController extends Controller {
public function actionValidate(){
$data = [
'Test'=>[
'username'=>'ybo',
'password'=>'123456',
'repassword'=>'123456',
'money'=>1.2,
'email'=>"2439953593@qq.com",
'sex'=>'男',
'zhengze'=>'ybo23',
'str'=>'111111666666'
]
];
$test = new \app\models\Test();
$test->load($data);
if(!$test->validate()){
print_r($test->getErrors());
}
// var_dump($test);
}
}
?>
![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAARYAAACcCAIAAAA1cxMSAAALkElEQVR4nO2dzWvkyBmH9RcE8nc05BSCTsMws7dA8B502mUPe1rj48JAErIC7wZyyextD2OfM4tm1OPgDgNmFhbP3LKeYXCDMZg4l+7z2GEObXW3clC3VKUqfZRef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jx+Jr8/Ui57sGiJ+qc82OUS2Inzkug5nZCpmhD2I5M1SJJZoh4kKKcaPhJtr0WWzr5i1q2Fhcgj7PDV1RxajkLBzCYWQWKIVJiqkGD8ShiF3iW6+lcRZhfIFp6mdPUfWL9zuXOouVO64iFtoBRMVUs1OCMMwDD0n1YlLKxTfcKCQWUChSgqJHbkcK/I6clKF4nSTbcYIEks0xiyFKsePCAqFecMJ6UuZzSQRhhOEneN9R44zkimExBKtMEshLbITSOPPWCuuHVCoBYWm7kgWQ1fxo6CQXpilUGcw4XK0GVAopB1QCAASUAgAElAIABINKoRCGVtQCIUiFRRCoUgFhVAoUkEhFIpUUAiFIhUUQqFIpayQKr7v+75/enq6WCyUGv70w5cli36SYRjiHRnQBLUp5Pv+eDw+Pz8v37CMHlAIaE6dCkUcHx/PZrMyDasqxARxjNypfGWomBzCrAjiWnmONXK9zZujjpfsKMsbQbIOSFO/QhEXFxeFDSkKyf8CNyeGPMGDy8OJ90wWpIrhO6kFcLiPAY76FTo6Orq6uirTkHYXkn2Xed3nKyQNZ9x+za5nxY0ICPwfmQ5Fg0RBADkAAAAASUVORK5CYII=)
<?php
namespace app\models;
use yii\base\Model;
class Test extends Model {
public $username;
public $password;
public $repassword;
public $money;
public $email;
public $sex;
public $zhengze;
public $str;
public function rules(){
return [
// [['username','password'],'safe']
['username','required','message'=>'用户名不能为空'],
['password','required','message'=>'密码不能为空'],
// ['password','compare','compareValue'=>'123456','message'=>'不等于123456']
['password','compare','compareAttribute'=>'repassword','message'=>'两次密码不一致'],
['repassword','safe'],
['money','double','min'=>1.0,'max'=>2.0,'tooSmall'=>'小于最小值','tooBig'=>'大于最大值','message'=>'不是数字'],
['email','email','message'=>'邮箱格式不正确'],
['sex','in','range'=>['男','女'],'message'=>'不在选择范围中'],
['zhengze','match','pattern'=>'/^ybo\d{2,}$/','message'=>'不符合正则规则'],
['str','string','min'=>10,'max'=>20,'tooShort'=>'不能少于10位','tooLong'=>'不能大于10位','message'=>'不是一个字符串'],
['username','usernamefunction','params'=>['message'=>'不等于ybo']]
];
}
public function usernamefunction ($attribute,$params){ if($this->$attribute != 'ybo'){ $this->addError($attribute,$params['message']); } }}?>