Yii Framework Forum: trim all input data - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

trim all input data Rate Topic: -----

#1 User is offline   sluderitz 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 105
  • Joined: 26-February 09
  • Location:Germany

Posted 18 June 2009 - 01:33 PM

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.
0

#2 User is offline   eval 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 85
  • Joined: 25-November 08
  • Location:Athens - Greece

Posted 18 June 2009 - 02:14 PM

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
0

#3 User is offline   sluderitz 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 105
  • Joined: 26-February 09
  • Location:Germany

Posted 18 June 2009 - 03:05 PM

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.
0

#4 User is offline   eval 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 85
  • Joined: 25-November 08
  • Location:Athens - Greece

Posted 18 June 2009 - 03:29 PM

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

#5 User is offline   mousa 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 01-March 12

Posted 01 March 2012 - 08:52 AM

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.
0

#6 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 01 March 2012 - 09:41 AM

This topic is three years old, mind you.
"Less noise - more signal"
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users