Search code examples
jqueryformsyii2yii-extensionsmaskedinput

Yii2: validate masked input



I'm using a Yii2 MaskedInput plugin with a model for an integer field.

<?= $form->field($model, 'phone')->widget(MaskedInput::classname()([
    'mask' => '(999) 999-9999'
]); ?>

When I type text to this field, I'm getting an error (Phone must be an integer.)

Is there any way to validate this on the client and server without custom validation masks?


Solution

  • MaskedInput will only generate a text field. I presume that your phone field is being stored as an integer. You'll have to change your validation of the phone field to be text, rather than integer. You may need to change the field type in your database to be varchar(14)