Customize delete confirmation in GridView

As nearly everything in Yii 2, you are starting from scratch with out-of-the-box solution for displaying models in grids (using yii\grid\GridView). Usually last among columns ($columns) of such grid there is an button column (yii\grid\ActionColumn) that serves basic operations for corresponding row or item. One of such column’s buttons there’s a delete button.

Everything is ready for you, to be used without much effort, but only as long as you are building an English-only application, as translating or customizing delete confirmation in such scenario isn’t a super-easy task. One of a great approaches is given in here: How to create a custom delete button?.

But, it works just great, if you want to customize just a single delete button of a one or maybe two grid view. What about, if you have a grid view for each and every of your data model?

Read More “Customize delete confirmation in GridView”

One of two model’s attributes must be provided

Suppose that we have any Yii 2 model with to and from attributes. And we want to make sure that either of them is provided. So we need a logical OR relation between these two attributes.

A slightly modified Stack Overflow example will do the trick:

['from', 'required', 'when' => function($model) {
    return !is_numeric($model->to);
}, 'message' => Yii::t('models', 'Either "to" or "from" must be provided.')],

Using a closure (an anonymous function) is all the “magic” here. We use it as a value $when of property of yii\validators\Validator (actually yii\validators\RequiredValidator in this case).

To basically tell Yii 2 that it must validate whether from does not have null or empty value if, and only if to does not have null or empty value as well.