D
D
Dmitry Kim2016-02-17 08:11:07
Yii
Dmitry Kim, 2016-02-17 08:11:07

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);
}

In the controller through attaching behaviors to actions or in models through attaching behaviors during the beforeValidate event?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Kim, 2016-02-17
@kimono

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.

E
enchikiben, 2016-02-17
@EnChikiben

It's more logical to override the model's load method
www.yiiframework.com/doc-2.0/yii-base-model.html#l...

N
Nikita, 2016-02-17
@bitver

In the model

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question