Ch 5 - Create new project

When I load the ‘create project’ page all the form fields have a red * next to them signifying a required field. How can that be? I have not set any rules in the Project class at all. Attempting to create a new project gives me error messages for every field I didn’t fill in. See screen-shot below.

Post a copy of your rules function in your project model

for ex.


        public function rules()

        {

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

                // will receive user inputs.

                return array(

                        array('create_user_id, update_user_id', 'numerical', 'integerOnly'=>true),

                        array('name', 'length', 'max'=>128),

                        array('description, create_time, update_time', 'safe'),

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

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

                        array('id, name, description, create_time, create_user_id, update_time, update_user_id', 'safe', 'on'=>'search'),

                        array('name','required'),

                        array('description','required'),

                );

        }



Thanks, here it is:


public function rules()

	{

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

		// will receive user inputs.

		return array(

			array('name, description, create_time, create_user_id, update_time, update_user_id', 'required'),

			array('create_user_id, update_user_id', 'numerical', 'integerOnly'=>true),

			array('name', 'length', 'max'=>128),

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

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

			array('id, name, description, create_time, create_user_id, update_time, update_user_id', 'safe', 'on'=>'search'),

			//array('name', 'required'),

		);

	}

Ok, I see what happened. The author did not include full instructions when creating the database table on page 63. He did not indicate NULL or NOT NULL for any of the table columns (except for "id"). Therefore, when I followed along verbatim in the book I created the columns - using the parameters provided by the author - and left their default in phpmyadmin set to NOT NULL (since the author did not specify otherwise). When Gii generated the code, all the form fields were automatically configured as required (the first array in my code) because the table columns were set to NOT NULL.

I am surprised no one else ran into this problem. It’s not until page 82 - well after the fact - that the author mentions NULL and NOT NULL in the MySQL column definitions and how they affect the code Yii generates.