T
T
terkin2012-10-02 18:52:21
Yii
terkin, 2012-10-02 18:52:21

Validator 'safe'?

I recently started studying Yii, and I just can’t understand the meaning of this validator, that is, there is such a description in the class description - The CSafeValidator validator marks related attributes as safe so that they can be assigned in batches.
But unfortunately this is not clear to me, could you explain to me with your fingers, or with examples when you need to use this type of validator.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
W
wartur, 2012-10-02
@terkin

www.yiiframework.com/doc/guide/1.1/en/form.model
It's very simple. This is such a thing that allows the framework to understand that the given model attribute that came from the attributes (cmodel::attributes) is allowed to be assigned to model attributes without validation . This thing must be used with care.
e.g. $model->attributes = $_POST['Article'];

W
wartur, 2012-10-02
@wartur

Yes. The same thing will happen.
And I highly recommend you. do not use safe, it should be used when you need to process it with before/afterValidate. And use unsafe when, on the one hand, you need to validate an element before writing, and on the other hand, do not give the opportunity to write it through attributes.

P
p4s8x, 2012-10-02
@p4s8x

If you try to assign unsafe then Cmodel->validate() will return true anyway, Warning unsafe will be in the logs and this property will not be assigned - why?
1) In Yii applications, they do what is called massive assigment - $model->attributes = $_POST['User']
The model has system properties like isNewRecord, primaryKey, scenario.
If an attacker assigns $_POST['User']['scenario'], then it is not known what can happen in the application - this is the reason why safe \ unsafe are generally separated.
2) The next attack option is to send in a post request, for example $_POST['User']['is_admin']=1

when to use this type of validator.

For example for fields in mysql of type TEXT/LONGTEXT

F
Fredoss, 2012-10-02
@Fredoss

In a simple way, I would write the answer like this: use the safe validator when you don’t care what value the validated attribute can take on mass assignment :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question