A
A
Alexey2018-02-06 13:39:18
Yii
Alexey, 2018-02-06 13:39:18

A field for entering a phone number with a mask and country code?

Hello!
There is a field in the database for storing phones in the format
79998889998899 for a phone in Russia +7 999 888 999 88 99
819998889998899 for a phone in Japan +81 999 888 999 88 99
8529998889998899 for a phone in Hong Kong +852 999 888
999 in which the country code is selected from select, and a text field for the phone by mask.
With a mask, it’s more or less clear, but I can’t figure out how to bind select here in such a way that it looks like a single field and that validation takes place.

<?= $form->field($model, 'phone')
                    ->textInput(['placeholder'=>'(999) 999 9999'])
                    ->widget(\yii\widgets\MaskedInput::className(), [
                        'mask' => '(999) 999 9999',
                        'clientOptions' => [
                            'removeMaskOnSubmit' => true,
                        ],
                    ]) ?>

5a7985cef3f78414978229.jpeg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2018-02-06
@webinar

https://github.com/jackocnr/intl-tel-input
andr-04.github.io/inputmask-multi/ru.html
https://intl-tel-input.com/

V
Vladimir Soldatov, 2018-02-07
@TPbIHTPABA

Write your widget
Validator pattern /^((\+\d{1,3})[\- ]?)?(\(?\d{2,3}\)?[\- ]?)?[\ d\- ]{9,10}$/
Each country has its own input mask, respectively, for this we use the MaskedInput widget.
Render template to field using boostrap input-group (input-group-btn + dropdown-menu).
When selecting and initializing the widget, we change the input mask to the one selected from the dropdown-menu. Similar to the flag icon in input-group-btn.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question