How do I change an element into an array of elements in the model rules?

Hi,

I have the following rules in my model

array(‘first_name,name_of_principal_director_head_of_hr, grades_offered, location, years_offered, describe_your_core_business, email, profile_picture’, ‘length’, ‘max’=>255 ),

I have just changed my grades_offered to a multiple select dropdown on my form.

My question is…

How do I change grades_offered into an array of elements? I’ve tried this

array(‘first_name,name_of_principal_director_head_of_hr, location, years_offered, describe_your_core_business, email, profile_picture’,‘grades_offered’=>array(‘Gr00, Gr0, Gr1, etc’), ‘length’, ‘max’=>255 )

Thank you…

Leave your first rule unchanged, and add a new one:




array('first_name,name_of_principal_director_head_of_hr, grades_offered, location, years_offered, describe_your_core_business, email, profile_picture', 'length', 'max'=>255 ),

array('grades_offered','in','range'=>array('Gr00', 'Gr0', 'Gr1'), 'allowEmpty'=>false),

See this wiki: http://www.yiiframework.com/wiki/56/

Same thoughts as bennouna suggested.:)

Thanks for the quick response…

I have done that, now I get the following error:

"Register has an invalid validation rule. The rule must specify attributes to be validated and the validator name."

Please post your validation rules, and the complete error output.

public function rules()

{


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


	// will receive user inputs.


	return array(


		array('first_name,username, last_name, email, tel, active, role, password', 'required'),


                    array('first_name,username, last_name, email, tel, active, role, password', 'allowEmpty' => true, 'on'=>'editaccount'),


		array('first_name,name_of_principal_director_head_of_hr, location, grades_offered, years_offered, town_suburb_of_school_college_institution_company,describe_your_core_business, number_of_academic_lecturing_facilitation_staff_employed_on_an_a, number_of_learners_students_on_an_average_day, username, website_of_school_college_institution_company, phone_number_of_school_college_institution_company, name_of_school_college_institution_company, last_name, email, , profile_picture', 'length', 'max'=>255),


                    array('grades_offered','in','range'=>array('Gr00', 'Gr0', 'Gr1'), 'allowEmpty'=>false),


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


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


		array('id,username, first_name,active, second_name,location, tel,name_of_principal_director_head_of_hr, grades_offered, years_offered, number_of_learners_students_on_an_average_day,describe_your_core_business, number_of_academic_lecturing_facilitation_staff_employed_on_an_a, website_of_school_college_institution_company, phone_number_of_school_college_institution_company,town_suburb_of_school_college_institution_company, email, name_of_school_college_institution_company, cell, password', 'safe', 'on'=>'search'),


		array('image', 'file', 'types'=>'jpg, gif, png', 'maxSize'=>1024 * 1024 * 5, 'allowEmpty' => true),


		array('image', 'file', 'types'=>'jpg, gif, png', 'maxSize'=>1024 * 1024 * 5, 'allowEmpty' => true, 'on'=>'editaccount'),


                    //array('verifyCode', 'captcha', 'on'=>'register'),


                    //array('verifyCode', 'captcha', 'on'=>'insert','allowEmpty'=>!CCaptcha::checkRequirements()),


	);


}

Error :

Register has an invalid validation rule. The rule must specify attributes to be validated and the validator name.

133 * PHP setter magic method.

134 * This method is overridden so that AR attributes can be accessed like properties.

135 * @param string $name property name

136 * @param mixed $value property value

137 */

138 public function __set($name,$value)

139 {

140 if($this->setAttribute($name,$value)===false)

141 {

142 if(isset($this->getMetaData()->relations[$name]))

143 $this->_related[$name]=$value;

144 else

145 parent::__set($name,$value);

146 }

147 }

148

149 /**

150 * Checks if a property value is null.

151 * This method overrides the parent implementation by checking

152 * if the named attribute is null or not.

153 * @param string $name the property name or the event name

154 * @return boolean whether the property value is null

155 * @since 1.0.1

156 */

157 public function __isset($name)

Well in this line:


array('first_name,name_of_principal_director_head_of_hr, location, grades_offered, years_offered, town_suburb_of_school_college_institution_company,describe_your_core_business, number_of_academic_lecturing_facilitation_staff_employed_on_an_a, number_of_learners_students_on_an_average_day, username, website_of_school_college_institution_company, phone_number_of_school_college_institution_company, name_of_school_college_institution_company, last_name, email, , profile_picture', 'length', 'max'=>255),

Notice the double comma between [font="Courier New"]email[/font] and [font="Courier New"]profile_picture[/font]?

Reformatted:




public function rules()

{

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

    // will receive user inputs.

    return array(

        array('first_name,username, last_name, email, tel, active, role, password', 'required'),

        array('first_name,username, last_name, email, tel, active, role, password', 'allowEmpty' => true, 'on'=>'editaccount'),

        array('first_name,name_of_principal_director_head_of_hr, location, grades_offered, years_offered, town_suburb_of_school_college_institution_company,describe_your_core_business, number_of_academic_lecturing_facilitation_staff_employed_on_an_a, number_of_learners_students_on_an_average_day, username, website_of_school_college_institution_company, phone_number_of_school_college_institution_company, name_of_school_college_institution_company, last_name, email, , profile_picture', 'length', 'max'=>255),

        array('grades_offered','in','range'=>array('Gr00', 'Gr0', 'Gr1'), 'allowEmpty'=>false),

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

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

        array('id,username, first_name,active, second_name,location, tel,name_of_principal_director_head_of_hr, grades_offered, years_offered, number_of_learners_students_on_an_average_day,describe_your_core_business, number_of_academic_lecturing_facilitation_staff_employed_on_an_a, website_of_school_college_institution_company, phone_number_of_school_college_institution_company,town_suburb_of_school_college_institution_company, email, name_of_school_college_institution_company, cell, password', 'safe', 'on'=>'search'),

        array('image', 'file', 'types'=>'jpg, gif, png', 'maxSize'=>1024 * 1024 * 5, 'allowEmpty' => true),

        array('image', 'file', 'types'=>'jpg, gif, png', 'maxSize'=>1024 * 1024 * 5, 'allowEmpty' => true, 'on'=>'editaccount'),

        //array('verifyCode', 'captcha', 'on'=>'register'),

        //array('verifyCode', 'captcha', 'on'=>'insert','allowEmpty'=>!CCaptcha::checkRequirements()),

    );

}




Thanks a lot bennouna,

I noticed that. I achieved the same results using JQuery and php (implode and explode) functions. http://api.jquery.com/val/:

FIXED…:)

Thank you