代码之家  ›  专栏  ›  技术社区  ›  Moeez

Yii2-匹配模式工作不正常

  •  0
  • Moeez  · 技术社区  · 7 年前

    我有一个名为 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 回复  |  直到 7 年前
        1
  •  3
  •   mleko    7 年前

    您的模式无效(缺少分隔符),而且它只匹配单个数字,添加 + 标记您要允许超过一位数

    尝试

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