0

I have a column named imsi which is a string and I want to add only numbers in it so for that I used

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

Now, when I try to create a new record and insert the imsi number with or without numbers it will always give me the error message.

enter image description here

My model is

 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']],
    ];
}

How can I exactly match the pattern?

Any help would be highly appreciated.

Moeez
  • 494
  • 9
  • 55
  • 147

1 Answers1

3

Your pattern is not valid(you are missing delimiters), also it matches only single digit, add + to mark that you want to allow more than single digit

Try

[['imsi'], 'match','pattern'=> '/^[0-9]+$/','message'=>'IMSI must include only numbers'],
mleko
  • 11,650
  • 6
  • 50
  • 71