S
S
Sergey Vasiliev2020-12-08 02:56:24
Yii
Sergey Vasiliev, 2020-12-08 02:56:24

How to implement options for displaying model fields depending on the value of a global variable in Yii2?

There are two tables: user and contact. The user table has a user->some_value field. It can be 0 or 1. Depending on what this value is, I need to receive formatted data when querying for data from the contact table. Example: if the value of the field user->some_value === 0, then the field contact->name should be formatted according to a certain condition preg_replace (one formatting option), and if user->some_value === 1, then preg_replace (another formatting option) . I can certainly do this check in the view file, but this rule applies to the entire project. I can’t find it on the internet: either it should be written in rules, or in behaviors. Help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bashcode, 2020-12-30
@bashcode

Based on the mvc ideology, the question belongs to the view category (if there is no other level of logic, for example, validation, or, say, replacing the model passed to the form).
Those. best to write there.
In this case, it is better to break the view into parts, depending on the conditions.
Another option is to make a widget if there is a possibility of reusing this case in other places and projects.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question