This is my first post on this forum, so Hello Everybody
I have one problem regarding CHtmlPurifier and length rules.
My code is as follows:
public function rules()
{
$purifier = new CHtmlPurifier();
$purifier->options = array('HTML.Allowed' => '',);
return array(
[...]
array('text', 'filter', 'filter' => array($purifier, 'purify')),
array('text', 'length', 'max' => 3000, 'min' => 2),
);
}
When I’m trying to save object with text e.g.
<img src="#" />
the first rule makes it empty, which is good. Then I supposed that ‘length’ rule should return validation error, because in that step ‘text’ is shorter than minimum = 2. Unfortunately it doesn’t and I end with new database record with empty ‘text’ field.
My idea was to validate length after filtering unallowed tags.
Do you have any thoughts why above code doesn’t work as I expected? Any ideas how to bypass this?
Regards,
KS
[edit] - Found the answer
It’s funny, but I’ve already found the answer. It’s a little illogical, but ‘length’ rule with e.g. ‘min => 2’ won’t fail for empty string. It has to have ‘allowEmpty’ option set to false. So my code should look like below:
public function rules()
{
$purifier = new CHtmlPurifier();
$purifier->options = array('HTML.Allowed' => '',);
return array(
[...]
array('text', 'filter', 'filter' => array($purifier, 'purify')),
array('text', 'length', 'max' => 3000, 'min' => 2, 'allowEmpty' => false),
);
}