Yii2-匹配模式无法正常工作

时间:2018-04-05 04:35:53

标签: php input yii2 yii2-advanced-app yii2-validation

我有一个名为imsi的列string,我想只在其中添加数字,所以我使用了

[['imsi'], 'match','pattern'=> '[0-9]','message'=>'IMSI must include only numbers'],

现在,当我尝试创建新记录并插入带有或不带数字的imsi号码时,它总会给我错误信息。

enter image description here

我的模特是

 public function rules()
{
    return [
        [['imsi','operator_name','data_details','sms_details','status'], 'required'],
        [['created_by', 'updated_by', 'sim_stauts', 'issued_to', 'returned_by', 'historic','data_details'
            ,'sms_details','monthly_bill','bill_date','credit_limit'], 'integer'],
        [['created_at', 'updated_at','returned_at'], 'safe'],
        [['imsi'], 'match','pattern'=> '[0-9]','message'=>'IMSI must include only numbers'],
        [['operator_name'], 'string', 'max' => 20],
        [['sim_number', 'status','plan_name','imsi'], 'string', 'max' => 50],
        //[['monthly_bill'], 'string', 'max' => 100],
        //[['imsi'], 'unique'],
        [['created_by'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['created_by' => 'id']],
    ];
}

我如何完全匹配模式?

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

您的模式无效(您缺少分隔符),也只匹配单个数字,添加+以标记您要允许多个数字

尝试

[['imsi'], 'match','pattern'=> '/^[0-9]+$/','message'=>'IMSI must include only numbers'],