How to trim a field for a model?


I am very new to Yii and am coming from CodeIgniter. I’ve just starting out with 1 table in SQLite so I can see what is going on, etc.

I have noticed that the fields do not trim/strip whitespaces and I’m wondering how I can do this.

In the docs it says I have to use CFilterValidator but I am unsure of how to use it.

My rules inside the ‘Workers’ model:

	public function rules()


		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('name', 'required'),

			array('birthMonth, age, weight', 'numerical', 'integerOnly'=>true),

			array('wage', 'numerical'),

			array('nickname, gender, picture, speaks, nationality', 'safe'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, name, nickname, gender, birthMonth, age, weight, picture, speaks, nationality, wage', 'safe', 'on'=>'search'),



I tried:

array(‘name’, ‘required’, ‘trim’)

This doesn’t work and gives me an error.

I tried:

array('name', 'required', 'filter'=>trim)

This doesn’t work and gives me an error.

I tried, following this comment:

To do this:

array('name', 'required', 'filter'=>array($this, 'trim')),

This gives me an error:

Property "CRequiredValidator.filter" is not defined.

All I want to do is trim the whitespace from the field.

How can I best achieve this? And why isn’t whitespace taken care for me?


Try this

array('name', 'filter', 'filter'=>'trim'),

Another option off the top of my head:

in Model

   public function beforeSave() {

      $this->name = trim($this->name);

      return parent::beforeSave();

   } //End beforeSave()

The trim() assignment may need to use


I have used the setAttribute() to set ‘NULL’ if the attribute was empty, so I’m not sure which is better/correct/etc :(

Hm, maybe I can be some wrong. Try also this

array('name', 'filter', 'filter'=>array($this, 'trim')),

But anyway, please refer to page


I think that you wrote bad solution…

This is the correct syntax.

array('name', 'filter', 'filter'=>'trim'),

The below code will try to execute a method trim() declared in the current class.

What this means:

'filter'=>array($thisClass, $functionInThisClass())

Will this place a null, or an empty string, in ‘name’ when it is saved?

This will remove all the white spaces at the start & end of the string.

Hello there.

Diggy suggested;

array('name', 'filter', 'filter'=>array($this, 'trim')),

This gives me an error;

// Other attempts

I tried;

array('name', 'required', 'filter', 'filter'=>'trim'),


array('name', 'required', 'filter'=>'trim'),

This gives me the following error;

If, however I do this:

array('name', 'filter', 'filter'=>'trim'),

This works fine, but now the field is no longer required.

How do I make it so that it is required and still filters?

Thanks for all your help so far. I appreciate it as a Yii-newbie.

You just need to add two rules: first for required validation, second for trim’s string…

array('name', 'required'),

array('name', 'filter', 'filter'=>'trim'),

Hey that worked great! Thanks a lot!

I appreciate it.

Right! you have to write each validator and filter in a separate line.

Hi all

I almost ask something without a try, but maybe this helps to someone…

i had

array('area', 'required'),

array('area', 'unique'),

array('area', 'filter', 'filter'=>'trim'),

but the unique validation was not occurring.

So I put the unique validation after the trim filter and it worked:

array('area', 'required'),

array('area', 'filter', 'filter'=>'trim'),

array('area', 'unique'),


Happy hacking!

As extra help for future reference:

The reason of why an array is accepted as an input in filter validator is found here: