I think trimming should be done in beforeValidate. The reason for this is that some validations might even fail with surrounding spaces to an input. If an email field for example, will never validate if there are spaces around the actual email address. So, beforeValidate makes sure things are in proper shape before validation and off course it'll also be saved in database the same way.

But, I was wondering if there is a built in way to do this with Yii. There should be something like this out of the box. In Kohana it used to happen automatically. If I have a large set of inputs, doing this every time for each input could be time consuming.