[[+content_image]]
D
D
darknet372017-03-30 19:23:34
Yii
darknet37, 2017-03-30 19:23:34

How to specify validation in validation rules. Yii2?

I have this rule in my model:

[['name', 'email', 'phone', 'address'], 'required'],

how do I tell the fields to be processed by htmlspecialchars ?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Dmitry, 2017-03-30
@darknet37

Good evening.
Why do you need htmlspecialchars when writing to the database?
Another thing is when outputting from the database to the site page, then yes, htmlspecialchars () is used.
But for this, yii2 has two methods, Html::encode and Html::decode() , and they just use htmlspecialchares().
So use the capabilities of the framework-a. When outputting to the browser, use Html::encode().

M
Maxim Timofeev, 2017-03-30
@webinar

And how do you have mandatory fields associated with htmlspecialchars?
There are events beforeSave, beforeValidate and others where you can make any transformations, what does the 'required' validator have to do with it?

N
Nikolai Konyukhov, 2017-03-31
@heahoh

No matter how strange it may sound, the area of ​​responsibility of the validator is data validation. The validator does not change the data, it only tells the model whether it stores the correct data.
If you want validation to modify the data - don't use a framework - use the best practices of global variables, spaghetti code, GOD objects, magic numbers and other technologies

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question