Determine attribute type in model event

In beforeSave, afterSave, … is there a way to determine and act upon all the number attributes? Or string attributes? Or required attributes? You get the idea of what I’d like to be able to do, identify attribute by their rule and then perform an action, str_replace, …

As always, thank you for taking the time to help!

You can use yii\db\ColumnSchema for this. ColumnSchema gives you a lot of info about a db table column and it’s types:

foreach($this->attributes as $attribute){
    $column = static::getTableSchema()->getColumn($attribute);
    if ($column !== null){
         $dbType = $column->dbType;
         $phpType = $column->phpType;
         $type = $column->type;

$phpTypes can be - string, boolean, integer, double, array
$types can be - char, string, text, boolean, smallint, integer, bigint, float, decimal, datetime, timestamp, time, date, binary, and money.

For rules that’s a bit tricky. But you can use $this->getActiveValidators($attribute) method of ActiveRecord to determine what rules are applied to an attribute.

What is your goal?
Because this should usually be solved by rules themselves. Give a look at Filter core validator.

If this won’t solve your problem, I’d suggest you to create a Trait with the transformations you’d like to perform, attach it to the model and call the trait transformations on beforeSave, afterSave, or wherever needed.