FilterValidator is actually not a validator but a data processor. It invokes the specified filter callback to process the attribute value and save the processed value back to the attribute. The filter must be a valid PHP callback with the following signature: php function foo($value) { compute $newValue here return $newValue; } Many PHP functions qualify this signature (e.g. trim()). To specify the filter, set [[filter]] property to be the callback.
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends Validator
Example #1
0
 public function __construct($config = [])
 {
     $filter = function ($value) {
         return trim(mb_strtolower($value));
     };
     parent::__construct(ArrayHelper::merge($config, compact('filter')));
 }
 /**
  * @inheritdoc
  */
 public function init()
 {
     $this->filter = function ($value) {
         $value = strip_tags($value, $this->allowedTags);
         return RichTextPurifier::process($value);
     };
     parent::init();
 }
Example #3
0
 public function testValidateAttribute()
 {
     $m = FakedValidationModel::createWithAttributes(['attr_one' => '  to be trimmed  ', 'attr_two' => 'set this to null', 'attr_empty1' => '', 'attr_empty2' => null, 'attr_array' => ['Maria', 'Anna', 'Elizabeth'], 'attr_array_skipped' => ['John', 'Bill']]);
     $val = new FilterValidator(['filter' => 'trim']);
     $val->validateAttribute($m, 'attr_one');
     $this->assertSame('to be trimmed', $m->attr_one);
     $val->filter = function ($value) {
         return null;
     };
     $val->validateAttribute($m, 'attr_two');
     $this->assertNull($m->attr_two);
     $val->filter = [$this, 'notToBeNull'];
     $val->validateAttribute($m, 'attr_empty1');
     $this->assertSame($this->notToBeNull(''), $m->attr_empty1);
     $val->skipOnEmpty = true;
     $val->validateAttribute($m, 'attr_empty2');
     $this->assertNotNull($m->attr_empty2);
     $val->filter = function ($value) {
         return implode(',', $value);
     };
     $val->skipOnArray = false;
     $val->validateAttribute($m, 'attr_array');
     $this->assertSame('Maria,Anna,Elizabeth', $m->attr_array);
     $val->skipOnArray = true;
     $val->validateAttribute($m, 'attr_array_skipped');
     $this->assertSame(['John', 'Bill'], $m->attr_array_skipped);
 }
 /**
  * @inheritdoc
  */
 public function init()
 {
     $this->filter = array($this, 'purify');
     return parent::init();
 }
 /**
  * @inheritdoc
  */
 public function init()
 {
     $this->filter = array($this, 'replace');
     $this->Essence = new Essence($this->essenceConfig);
     return parent::init();
 }
 /**
  * @inheritdoc
  */
 public function init()
 {
     $this->filter = array($this, 'texturize');
     $this->staticTranslations = array_merge(self::$DEFAULT_STATIC_TRANSLATIONS, ['``' => $this->leftDoubleQuote, '\'\'' => $this->rightDoubleQuote]);
     if (!is_array($this->noTexturizeTags)) {
         $this->noTexturizeTags = self::$DEFAULT_NO_TEXTURIZE_TAGS;
     }
     $this->prepareDynamicTranslations();
     return parent::init();
 }
Example #7
0
 public function rules()
 {
     return [['name', FilterValidator::className(), 'filter' => 'strip_tags'], ['name', 'string', 'max' => 255], ['name', 'required']];
 }