Yii2 rules 自定义验证规则
public function rules() { return[ [['groupname','bandwidth'],'required'],//不为空验证 [['id','bandwidth'], 'integer'],//整数验证 [['note'], 'safe' ],//安全验证 [['groupname'], 'unique'],//唯一验证 [['groupname'], 'string', 'length' => [1, 64]],//字符串验证 [['groupname'], 'GroupNameVerification'],//自义定验证 ]; } public function attributeLabels() { return [ 'id' => 'id', 'groupname' => Yii::t('app', 'speedlimitgroupname'), 'bandwidth' => Yii::t('app', 'speedlimitbandwidth'), 'note' => Yii::t('app', 'note'), ]; } /** * 自义定验证 */ public function GroupNameVerification($attribute){ if(!preg_match("/^(([a-z]+[0-9]+)|([0-9]+[a-z]+))[a-z0-9]*$/i",$this->groupname)){ return $this->addError($attribute, Yii::t('app', 'canonlycontainlettersandnumbers')); } }