-
Notifications
You must be signed in to change notification settings - Fork 4
/
PurifyBehavior.php
47 lines (41 loc) · 1.14 KB
/
PurifyBehavior.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace yii2mod\behaviors;
use yii\base\Behavior;
use yii\db\ActiveRecord;
use yii\helpers\HtmlPurifier;
class PurifyBehavior extends Behavior
{
/**
* @var array attributes
*/
public $attributes = [];
/**
* @var null The config to use for HtmlPurifier
*/
public $config;
/**
* Declares event handlers for the [[owner]]'s events.
*
* Child classes may override this method to declare what PHP callbacks should
* be attached to the events of the [[owner]] component.
*
* The callbacks will be attached to the [[owner]]'s events when the behavior is
* attached to the owner; and they will be detached from the events when
* the behavior is detached from the component.
**/
public function events()
{
return [
ActiveRecord::EVENT_BEFORE_VALIDATE => 'beforeValidate',
];
}
/**
* Before validate event
*/
public function beforeValidate()
{
foreach ($this->attributes as $attribute) {
$this->owner->$attribute = HtmlPurifier::process($this->owner->$attribute, $this->config);
}
}
}