4

I've got a question about Yii2's validation. So, my model validation rule's something like this:

return [
    ['status', 'required', 'on' => 'update'],
    [['status'], function ($attribute) {
        $this->$attribute = \yii\helpers\HtmlPurifier::process($this->$attribute);
    }],
];

The problem is that if the content is <script>alert('something')</script>, it will be blank due to purifier and the content will pass the required validation.

So how can I revalidate the content for require? Or what is the good way to do it?

chrki
  • 5,626
  • 6
  • 28
  • 51
Miller Newbie
  • 75
  • 1
  • 7

1 Answers1

10

Validation rules are processed one after another so just put the second one as first.

return [
    ['status', 'filter', 'filter' => function ($value) {
        return \yii\helpers\HtmlPurifier::process($value);
    }],
    ['status', 'required', 'on' => 'update'],
];
Bizley
  • 15,937
  • 5
  • 43
  • 53