1

It's boring to apply "trim" filter for each attribute in the model. Any idea does Yii2 have something like that:

['*', 'filter', 'filter' => 'trim'],
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68

2 Answers2

2

Well as far as i know you have to specify the attributes in order to apply any core validators like required or filter in the following way

[['attribute_1', 'attribute_2'], 'filter','filter' => 'trim'],

but looks like you are getting lazy to write all their names there and want the filter validator to be applied for all the attributes automatically you need to use the php functions array_keys and implode like below

[[implode(',', array_keys($this->attributes))], 'filter','filter' => 'trim'],

Hope it helps you out

Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
0

If someone has more interesting ideas, please write.

public function beforeSave($insert)
{
    foreach ($this->getDirtyAttributes() as $attName => $attValue) {
        if ($attValue && self::getTableSchema()->columns[$attName]->phpType == 'string') {
            $this->{$attName} = trim($attValue);
        }
    }
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64