D
D
dev4002016-11-22 21:48:31
Yii
dev400, 2016-11-22 21:48:31

Is it possible to connect a validator in this way?

You need your own validator for the phone, and doing it directly in the registration model is not comme il faut, as you also need to validate when editing user data, recovering a password, etc. Made a standalone validator following the documentation

<?php
namespace app\components\validators;

use yii\validators\Validator;

class PhoneValidator extends Validator
{
    public function validateAttribute($model, $attribute)
    {

        $model->$attribute = str_replace(['+7', '(', ')' , ' '], '', $model->$attribute);

    }
}

But I did not find information about its connection. How to use it now? Like this?
['phone', 'app\components\validators\PhoneValidator']

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2016-11-22
@dev400

In Yii2 there is such a thing as filters,
well, as an option! and use it!
stackoverflow.com/questions/28168253/custom-valid...

M
Maxim Timofeev, 2016-11-22
@webinar

The logic of the validator is to check the data for compliance. And if you need to replace them, then you just need to use the beforeValidate () or beforeSave () method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question