It's boring to apply "trim" filter for each attribute in the model. Any idea does Yii2 have something like that:
['*', 'filter', 'filter' => 'trim'],
It's boring to apply "trim" filter for each attribute in the model. Any idea does Yii2 have something like that:
['*', 'filter', 'filter' => 'trim'],
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
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);
}
}
}