form validation using the required fields in the model

Here’s my actionCreate:




...

public function actionCreate()

	{

		$model=new Profiles;

		$user = new Users;

		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Profiles']))

		{

			$model->attributes=$_POST['Profiles'];

			$user->attributes=$_POST['Users'];

			$user->username=$model->faculty_id_no;

			$user->salt=genRandomString();

			$this->tmpPassword = genRandomString();

			$user->password=Users::encrypting($model->tmpPassword ,$user->salt);

			$user->createtime=time();

			$user->lastvisit=time();

			$user->email=$model->contact_email;

			if($model->validate() && $user->validate())	{

				if($user->save())	{

					$model->user_id=$user->id;

					if($model->save())	{

						$this->redirect(array('view','id'=>$model->user_id));

					}

				}

			}

		}


		$this->render('create',array(

			'model'=>$model,

			'user'=>$user,

		));

	}

...



And in my create view file:




...

		<div class="row">

			<?php echo $form->labelEx($model,'faculty_id_no'); ?>

			<?php echo $form->textField($model,'faculty_id_no',array('size'=>20,'maxlength'=>20)); ?>

			<?php echo $form->error($model,'faculty_id_no'); ?>

			

			<?php echo $form->labelEx($user,'role'); ?>

			<?php echo $form->dropDownList($user,'role',Users::showRoles(),array('prompt'=>'&lt; Select a Role &gt;')); ?>

			<?php echo $form->error($user,'role'); ?>

		</div>

...



This is the picture that happens when I click submit:

1250

Untitled.jpg

, expecting that all the required fields declared will be checked. Apparently, what I expected was incorrect.

So how should I do this? The form should not be saved if the role field, which comes from a different model, is empty.

Thanks for your help in advance.

Can we see your "rules()" function?

Thanks for the reply rymonator.

Sorry I forgot to put my rules. Here it is:

Users model




	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

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

		// will receive user inputs.

		return array(

			array('username, password, email, role, salt', 'required'),

			array('createtime, lastvisit, role, status', 'numerical', 'integerOnly'=>true),

			array('username', 'length', 'max'=>20),

			array('username', 'length', 'min'=>1),

			array('password, email, salt', 'length', 'max'=>128),

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

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

			array('id, username, password, email, createtime, lastvisit, role, status, salt', 'safe', 'on'=>'search'),

		);

	}



Not sure if this is needed, but here is my Profiles model rules:




/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

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

		// will receive user inputs.

		return array(

			//required fields

			array('faculty_id_no,last_name,first_name,middle_name,contact_email,birthday' ,'required'),

			

			//integer only

			array('user_id, religion_id, present_municipality_id, present_province_id, present_zip, provincial_municipality_id, provincial_province_id, provincial_zip, sss_no, tax_id_no, hdmf_no, philhealth_no, gsis_no, status', 'numerical', 'integerOnly'=>true),

			

			//lengths

			array('faculty_id_no', 'length', 'max'=>20,'min'=>3),

			array('last_name, first_name, middle_name, present_home, present_district, contact_home, contact_mobile, contact_email, provincial_house, provincial_district', 'length', 'max'=>50),

			array('nickname', 'length', 'max'=>10),

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

			array('gender', 'length', 'max'=>1),

			array('civil_status_code', 'length', 'max'=>2),

			array('blood_type', 'length', 'max'=>5),

			

			//validators

			array('contact_email', 'email'),

			array('contact_email,faculty_id_no', 'unique'),

			array('birthday', 'type','type'=>'date','dateFormat'=>'yyyy-mm-dd','message'=>'{attribute} must be in the format of "YYYY-MM-DD".'),

			

			

			//set default

			array('religion_id,present_municipality_id,present_province_id,provincial_municipality_id,provincial_province_id', 'default','setOnEmpty'=>NULL),

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

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

			array('user_id, faculty_id_no, last_name, first_name, middle_name, nickname, birthday, birth_place, gender, civil_status_code, religion_id, blood_type, present_home, present_district, present_municipality_id, present_province_id, present_zip, contact_home, contact_mobile, contact_email, provincial_house, provincial_district, provincial_municipality_id, provincial_province_id, provincial_zip, sss_no, tax_id_no, hdmf_no, philhealth_no, gsis_no, status', 'safe', 'on'=>'search'),

		);

	}