use yii\base\DynamicModel;
// 创建一个动态模型
$model = DynamicModel::validateData(['name' => 'John', 'age' => 25], [
[['name', 'age'], 'required'],
['age', 'integer', 'min' => 18],
]);
// 使用动态模型进行数据验证
if ($model->validate()) {
echo "Validation passed!";
} else {
echo "Validation failed!";
print_r($model->getErrors());
}
[['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息']; #说明:CRequiredValidator 的别名, 确保了特性不为空.
['email', 'email']; #说明:CEmailValidator的别名,确保了特性的值是一个有效的电邮地址.
[['字段名'],match,'pattern'=>'正则表达式','message'=>'提示信息'];
[['字段名'],match,'not'=>ture,'pattern'=>'正则表达式','message'=>'提示信息']; /*正则取反*/ #说明:CRegularExpressionValidator 的别名, 确保了特性匹配一个正则表达式.
['website', 'url', 'defaultScheme' => 'http']; #说明:CUrlValidator 的别名, 确保了特性是一个有效的路径.
['verificationCode', 'captcha']; #说明:CCaptchaValidator 的别名,确保了特性的值等于 CAPTCHA 显示出来的验证码.
['description', 'safe'];
['age', 'compare', 'compareValue' => 30, 'operator' => '>=']; #说明:compareValue(比较常量值) - operator(比较操作符) #说明:CCompareValidator 的别名,确保了特性的值等于另一个特性或常量.
array('down_days', 'compare', 'compareAttribute'=>'up_days','message'=>'与上调设置天数不一致')
['age', 'default', 'value' => null]; #说明:CDefaultValueValidator 的别名, 为特性指派了一个默认值.
['username', 'exist']; #说明:CExistValidator 的别名,确保属性值存在于指定的数据表字段中.
['primaryImage', 'file', 'extensions' => ['png', 'jpg', 'gif'], 'maxSize' => 1024*1024*1024]; #说明:CFileValidator 的别名, 确保了特性包含了一个上传文件的名称.
[['username', 'email'], 'filter', 'filter' => 'trim', 'skipOnArray' => true]; #说明:CFilterValidator 的别名, 使用一个filter转换属性.
['level', 'in', 'range' => [1, 2, 3]]; #说明:CRangeValidator 的别名,确保了特性出现在一个预订的值列表里.
['username', 'unique'] #说明:CUniqueValidator 的别名,确保了特性在数据表字段中是唯一的.
['age', 'integer'];
['salary', 'number'];
['salary', 'double'];
1
[['from', 'to'], 'date'];
1
['username', 'string', 'length' => [4, 24]];
1
['字段名', 'boolean', 'trueValue' => true, 'falseValue' => false, 'strict' => true]; #说明:CBooleanValidator 的别名
#设置场景
public function scenarios()
{
$scenarios = parent::scenarios();
//各个场景的活动属性
$scenarios['register'] = ['useraccount', 'username', 'password','password_compare'];
$scenarios['save'] = ['useraccount', 'username', 'password'];
$scenarios['login'] = ['useraccount','password','verifyCode'];
$scenarios['dologin'] = ['useraccount','password'];
return $scenarios;
}
public function actionRegister() {
$model = new User();
$model->setScenario('register');
$id = Yii::$app->user->id;
// var_dump($id);
return $this->render('register',['model' => $model]);
}
array('name', 'rulesCheckLength','max'=>50,'message'=>'规则名称需小于2个字符'),
在model添加对应方法:
public function rulesCheckLength($attribute,$params){
$str = $this->$attribute;
$max = isset($params['max'])&&$params['max']?$params['max']:50;
if(function_exists('mb_strlen'))
$length = mb_strlen($str, Yii::app()->charset);
else
$length=strlen($str);
if ($length > $max) {
$this->addError($attribute, $params['message']);
return true;
}
return false;
}
// a1 needs to be unique ['a1', 'unique'] // a1 needs to be unique, but column a2 will be used to check the uniqueness of the a1 value ['a1', 'unique', 'targetAttribute' => 'a2'] // a1 and a2 need to be unique together, and they both will receive error message [['a1', 'a2'], 'unique', 'targetAttribute' => ['a1', 'a2']] // a1 and a2 need to be unique together, only a1 will receive error message ['a1', 'unique', 'targetAttribute' => ['a1', 'a2']] // a1 needs to be unique by checking the uniqueness of both a2 and a3 (using a1 value) ['a1', 'unique', 'targetAttribute' => ['a2', 'a1' => 'a3']]
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。