Answer the question
In order to leave comments, you need to log in
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);
}
}
['phone', 'app\components\validators\PhoneValidator']
Answer the question
In order to leave comments, you need to log in
In Yii2 there is such a thing as filters,
well, as an option! and use it!
stackoverflow.com/questions/28168253/custom-valid...
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 questionAsk a Question
731 491 924 answers to any question