Answer the question
In order to leave comments, you need to log in
What is the best way to filter data from user in Yii2?
Sample code:
--------------------------- IN controller behavior -----------------------------
$post = Yii::$app->request->post();
foreach ($post as $key => $value){
$post[$key] = удалитьЛишниеПробелыИДвойныеПустыеСтрокиВТекстовыхДанных($value);
}
Yii::$app->request->bodyParams = $post;
--------------------------- OR in model behavior------------------------------
foreach ($this->owner->attributes() as $attributeName => $attributeValue){
$this->owner->{$attributeName} = удалитьЛишниеПробелыИДвойныеПустыеСтрокиВТекстовыхДанных($attributeValue);
}
Answer the question
In order to leave comments, you need to log in
In general, the solution turned out to be this: make a filter-validator and hang it on the fields in the rules model. Then we will save ourselves from extra empty space both when receiving data from the user and from ourselves.
It's more logical to override the model's load method
www.yiiframework.com/doc-2.0/yii-base-model.html#l...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question