How to avoid the "NULL" string by default in form fields ?


I have some fields in my database with NULL as their default value. With the form created by the yiic command, these fields display by default the “NULL” string. I would want to display an empty string instead. How can i do that ? I browse the docs but i can’t find anything related to this subject.

Thank you very much.



you have to change Null to Not Null and set the default value to an empty string in you database. use phpmyadmin or something to edit you field in your tables.

Yes, it’s a solution. But i was wondering if there is an other solution on the Yii side ? May be like a post-filter which would automatically convert the “NULL” string to the empty string if the SQL value is null before displaying the form.

In my case, i can modify my database structure so i could indeed change the default value in my table. But sometimes you can’t make this kind of change.

Thank you.



i don’t think so. because the database puts the NULL value into the fields and not Yii.

Sure but Yii reads the fields values from the database. So, there is no way to convert the fields values read in the database by Yii just before the display in the form ? Something like this :

DB values -> Yii read these values -> ** Filter to convert null to empty string ** -> Form display (without any null)

I’m not sure to be very clear in my explanation… :slight_smile:

i’m sorry i was thinking about inserting data into the database. maybe you can use the afterFind() method to replace NULL values by an empty string.

Ah, yes, that should do the trick. I wasn’t aware of these AR methods.

Thank you very much for your help.