G
G
Goodlikee2018-06-18 10:04:08
Validation
Goodlikee, 2018-06-18 10:04:08

Yii2 Field validation with input mask?

I ran into a problem with validating a field with an input mask on a Yii2 site. I
output it as an ActiveForm
<?php echo $form->field($model, 'phone')->widget(\yii\widgets\MaskedInput::className(), [
'mask' => '+7 (999) 999 99 99',
'options'=>[
'id' => 'phone3',
'placeholder' => 'Phone',
'class'=>'callback-form__input '
]
])->label(false);?>
In the model, these validation rules are
public function rules()
{
return [
[['name', 'phone', ], 'required'],
[[ 'created_at', 'updated_at', 'status','view'], 'integer'],
[['phone'], 'string','min'=>'11', 'max' => 255 ],
[['name'], 'string', 'max' => 155],
];
}
If you enter at least 1 character in the phone input fields, the form is sent, if you specify the minimum length of 20 characters in the validation rules, for example, then when you try to send it, it gives an error, The “Phone” value must contain at least 20 characters. How to check fields for phone?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question