Yii2-匹配模式无法正常工作
Yii2- match pattern not working properly
我有一个名为 imsi
的列,它是一个 string
,我只想在其中添加数字,因此我使用了
[['imsi'], 'match','pattern'=> '[0-9]','message'=>'IMSI must include only numbers'],
现在,当我尝试创建新记录并插入带或不带数字的 imsi
数字时,它总是会给我错误消息。
我的模特是
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']],
];
}
如何才能准确匹配模式?
非常感谢任何帮助。
您的模式无效(您缺少定界符),而且它仅匹配一位数字,添加 +
以标记您希望允许多位数字
尝试
[['imsi'], 'match','pattern'=> '/^[0-9]+$/','message'=>'IMSI must include only numbers'],
我有一个名为 imsi
的列,它是一个 string
,我只想在其中添加数字,因此我使用了
[['imsi'], 'match','pattern'=> '[0-9]','message'=>'IMSI must include only numbers'],
现在,当我尝试创建新记录并插入带或不带数字的 imsi
数字时,它总是会给我错误消息。
我的模特是
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']],
];
}
如何才能准确匹配模式?
非常感谢任何帮助。
您的模式无效(您缺少定界符),而且它仅匹配一位数字,添加 +
以标记您希望允许多位数字
尝试
[['imsi'], 'match','pattern'=> '/^[0-9]+$/','message'=>'IMSI must include only numbers'],