[[+content_image]]
D
D
Dmitry Kim2016-05-24 11:29:54
Yii
Dmitry Kim, 2016-05-24 11:29:54

How to correctly write conditions for the ExistValidator filter in YII2?

There is a validation rule.

[['content_id'], 'exist',
  'skipOnError'      => true,
  'targetClass'       => News::className(),
  'targetAttribute' => 'id',
  'filter'                 => [
    'is_active'              => true,
    'allow_comments' => true,
    // интересует это место:
    ['>=', 'created_at', new Expression('NOW() - INTERVAL 7 DAY')],
  ],
  'message' => 'Новость не найдена',
  'on' => [self::SCENARIO_DEFAULT, self::SCENARIO_SEND_COMMENT],
],

I don’t understand how to use three attributes correctly (in this situation, there is an error
PHP Warning 'yii\base\ErrorException' with message 'strtoupper() expects parameter 1 to be string, array given'
).

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
V
vyachin, 2016-06-13
@kimono

first option
'filter'=> function (ActiveQuery $query) {
$query->andWhere([]);
}
second option
'filter' => [
'is_active' => true,
'allow_comments' => true,
'created_at >= NOW() - INTERVAL 7 DAY',
],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question