Answer the question
In order to leave comments, you need to log in
Yii2. How to dynamically connect to the Validotor class "MyBehavior" (Yii2 Behavior) so that all built In Validators inherit it?
Hello Colleagues
Perhaps someone can help.
I want to dynamically connect the behavior "MyValidationBehavior" (Yii2 Behavior) to all validators of the yii\validators\Validator base class of my project.
I wrote a code that adds my "MyValidationBehavior" one to each class.
I add "MyValidationBehavior" for all built In Validators
foreach (\yii\validators\Validator::$builtInValidators as $builtInValidator)
{
$container->set($builtInValidator,
["as $builtInValidator" => ['class' => MyValidationBehavior::className(),]] );
}
$container = \Yii::$container;
//вариант 1
$container->set('\yii\validators\Validator',
["as Validator" => ['class' => MyValidationBehavior::className(),]]);
//вариант 2
$validator = $container->get('yii\validators\Validator');
$validator->attachBehavior('Validator', MyValidationBehavior::className());
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question