Empty Field Validation

hey can anyone tell me how to validate empty field.I need to validate my name text field.when update the value of the name text fied it cannot be blank.how to do that.help me pls.it is plessure to see the sample code for above matter

Hi you can set the secnario


 array('name','required','on'=>'create'),

      

 array('name', 'allowEmpty' => TRUE,'on'=>'update'),

Hi

If you want the name field to be required except update scenario you chould use this


array('name','required'),

array('name', 'allowEmpty' => true,'except'=>'update'),

But it will give the error

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

nw what am I do now .pls help me

Please post your ‘rules’ method

Hi,

public function rules()

{

 return array(


     array('name', 'required'),


 );

}

I hope this will solve your problems. This will validate name attribute during create and update.

Thanks

chandran nepolean


public function rules()

	{

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

		// will receive user inputs.

		return array(

			array('users_id', 'required'),

			array('primary_degree_country_id,is_locum, located_in_country_id, is_permanent, pref_location_id, users_id', 'numerical', 'integerOnly'=>true),

			array('work_start_date', 'safe'),

			array('doctor_type_id, work_start_date, primary_degree_country_id, located_in_country_id, is_locum, is_permanent, pref_location_id','required','message'=>'Cannot be Empty '),

		

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

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

			array('id, doctor_type_id, work_start_date, primary_degree_country_id, located_in_country_id, is_locum, is_permanent, pref_location_id, users_id', 'safe', 'on'=>'search'),

		);

	}

Hi this is my model validation rule.in contrller what am I do. what is mean by validate function in controller???

i created when user click update profile button that user (current login user) can see the saved information and I gave them facility to update theri profile.In that case when user keep empty field i want give msg to dont be empty this field like wise.that is my problem how do I do that.pls give me sample code for that.

thanx in advance :rolleyes:

Hi,

Please can you paste your actionupate method…i think problem will be in ur controller…

In rules method, just add your attribute to safe.

I hope this will solve your problem

Thanks

chandran nepolean


public function actionUpdatecontactdetails() {

		// echo Yii::app()->user->id;

		// exit;

		// $ucdmodel = UserContactDetails::model()->findByPk(Yii::app()->user->id);

		$ucdmodel = UserContactDetails::model ()->find ( 'users_id=:id', array (

				':id' => Yii::app ()->user->id 

		) );

		// print_r($ucdmodel);

		// die;

		

		if (isset ( $_POST ['UserContactDetails'] )) {

			if (! is_null ( $ucdmodel )) {

				

				$ucdmodel->attributes = $_POST ['UserContactDetails'];

				if ($ucdmodel->validate()){

				if ($ucdmodel->update ()) {

					

					$this->redirect ( 'index#tabs-quote-departments' );

					}

				}

			}

		}

	}

this is my controller method

pls help me.still it is confused

help me pls .stil it cannot be solved.

hey…can anyone help me pls.

when I put validation rule nothin display.pls help me

Hi,

Replace this…

if ($ucdmodel->validate()){

                            if ($ucdmodel->update ()) {


                                    


                                    $this->redirect ( 'index#tabs-quote-departments' );


                                    }


                            }

this is changed way…just try this… i hope it will solve ur problem

                            if ($ucdmodel->save ()) {


                                    


                                    $this->redirect ( 'index#tabs-quote-departments' );


                                    }

Thanks

chandran nepolean

thanx for your reply.bt it is not the answer.when I put that one display nothing only white screen.what am I do now? :(

try reverting all changes made from above suggestions and just add those rules:

array( ‘name’, ‘required’, ‘on’=>‘update’ ),

array( ‘name’, ‘safe’, ‘except’=>‘update’ ),

every rule must have validator name/class (second param) and provided rules lack this part…

also - you should just call save() instead of validate() and update(). save() will do the validation and choose proper storing method (insert/update) automatically.

I cant understand what the reason.still display anything.

thanx guys

:(

is there anything in application.log?

Hi,

I think u have some errors in yours saving.

try this print_r($ucdmodel->errors)

when u have error…it will not redirect to your requested page

Thanks

chandran nepolean

only display blank page.I don knw what the reason. :( :(

hey dont you ave any idea???.still I cannot find the sollution for that

try to comment out everything in action body, add “echo ‘done’;” at the end and check if it works. if so - uncomment your code piece by piece and check when problem arises. basic debugging with php…