Yii框架中rules的一些常见写法总结

/**
* 规则
*/
public function rules()
{
return [
[['real_name','cert_type','cert_num','gender','phone','user_cert1','user_cert2','user_cert3','is_newsign','house_type'],'safe'],
[['cert_type','cert_num','real_name'], 'required','on' => self::SCENARIO_INSERT],
[['cert_type','cert_num','real_name','gender','phone'], 'required','on' => self::SCENARIO_FORSIGN],
['cert_type', 'default', 'value' => 0],
['cert_type','in', 'range'=>Yii::$app->params['allowCertTypeList'],'on' => [self::SCENARIO_UPDATE,self::SCENARIO_INSERT]],
['cert_type','in', 'range'=>[1,2,6,13],'on' => self::SCENARIO_FORSIGN],
['gender','in', 'range'=>Yii::$app->params['allowGenderList']],
['cert_num', function($attribute) {
if (isset($this->$attribute) && !isset($this->cert_type)) {
$this->addError($attribute, '证件类型和证件号码不匹配');
}
$is_cert = \apis\modules\account\models\logics\CertLogic::validateCertNum($this->cert_type,$this->$attribute);
if(!$is_cert){
$this->addError($attribute, '证件类型和证件号码不匹配');
}
}],
['cert_type', function($attribute) {
if($this->$attribute != 1 && (empty($this->user_cert1) || empty($this->user_cert2) || empty($this->user_cert3))){
$this->addError($attribute, '非身份证用户请上传证件照!');
}
},'on' => self::SCENARIO_FORSIGN],
[['user_cert1','user_cert2','user_cert3',],'filter', 'skipOnEmpty'=>true, 'filter'=>function($data){
return \common\models\FnModel::handleUUidToUrl($data);
}],
['real_name', function($attribute) {
$is_name = \apis\modules\account\models\logics\CertLogic::validateRealName($this->real_name);
if(!$is_name){
$this->addError($attribute, '真实姓名不符合规范');
}
},'on' => self::SCENARIO_FORSIGN],
];
}
用到了Yii场景的应用,详见:https://ask.zkbhj.com/?/question/4

0 个评论

要回复文章请先登录注册