trim all input data

What is the best way to apply the trim() function to all input data automatically in Yii? Currently all trailing and leading spaces end up in my database.

I suppose you could:

  • use validation to check for such things before saving the record

or

  • use beforeSave() to transparently trim data before saving…

just a thought

Thanx, overriding the beforeSave() method seems the best way, though I was hoping that there is some configuration setting in Yii where I could specify general filters for inputs.

Indeed, beforeSave() is what i meant, i will edit the above to correct the typo.

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.

This topic is three years old, mind you.