Manipulation data before storage

Hep hey!

I got a nice little simple gii generated application which i need to make sure saves data in the right format.

As it is now, the user is allowed to write white space when entering new table entries but when i have to work with the data i need to replace the whitespace with a "-".

Now, i want to change the format of the data to contain "-" in place of white space on insert and update.

Question is, how do i do that?

  1. I would make a rule whice states that the use need to supply "-" where there are white space but i want it to be a smooth experience where the system handles small trival details like that in the background.

  2. I need to parse the string and replace white space before sending the data to the DB but i have no idea where i can do that.

  3. Whatever good idea you can come up with :rolleyes:

Thanks in advance!

(Reason for no code is that its all trival code generated by gii, just using the model and CRUD generator)

If you want to force the user to insert valid data, you can sue CRegularExpressionValidator, for example like that:

array('entry', 'match', 'pattern'=>'/^[A-Za-z0-9-]+$/'),

This allows letters, numbers and -.

If you want to allow user to insert space and then you will replace (I don’t like personally this solution), you can edit before validating with strreplace.

I prefer to show the error and let the user to fix it, I always hate to took decision for the user.

I know what you mean, i usually let the user correct the info so that it works with the program, but as there are no performance demands and it just has to be nice and easy to use, i opted for the idea of fixing it for them.

And thanks for your awnser, ill give it a crack!

Just use a string replace function like

function replace_space($data)


return str_replace(" ", "-", "$data");


and call it in your controller