Yii Framework Forum: Ch 5 - Create new project - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Ch 5 - Create new project all fields required?

#1 User is offline   David Crisler 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 05-April 11

Posted 14 April 2011 - 12:50 PM

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.

Posted Image
0

#2 User is offline   Topher 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 14-April 11

Posted 14 April 2011 - 07:07 PM

View PostDavid Crisler, on 14 April 2011 - 12:50 PM, said:

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'),
                );
        }

0

#3 User is offline   David Crisler 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 05-April 11

Posted 15 April 2011 - 10:39 AM

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'),
		);
	}

0

#4 User is offline   David Crisler 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 05-April 11

Posted 15 April 2011 - 11:11 AM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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