Adderror() And Adderrors()

hey pls help me.how to use addError() to display error.pls tell me with examples

Hi,

This should be in your model

$this->addError(‘username’,‘This user has already been added to the programm.’);

In view

<?php echo $form->errorSummary($model); ?>

I hope this will help to solve your problem.

Thanks

chandran nepolean

where do I put this code in the model.which function.tha’s why I want to understand


$this->addError('username','This user has already been added to the programm.');



Hi,

I cannot mention this detail, because its depends on your method validation. Mostly u can use this in your validation method.

Thanks

chandran nepolean

hi I used this one in my contraller to get the error mesage.bt I need to convert it in to string.How Do I do it


$usermodel->getErrors();

Hi

You can add error to an model like





$Modelobject->addError('attributeName','the error to show');




if you are in the model class You can do like





$this->addError('attributeName','the error to show');




Hi,

I hope this helps you.

			$errors = array();


			foreach ($authItem->getErrors() as $attribute=>$attributeErrors)


				foreach ($attributeErrors as $error)


					$errors[] = array(


						'attribute'=>$attribute,


						'label'=>$authItem->getAttributeLabel($attribute),


						'error'=>$error


					);


			$response = compact('errors');


		}


		header('Content-type: application/json');


		echo CJSON::encode($response);

[size=2]then how do I create my view.it is somwhat confuse to me and thanx your reply[/size]

can u give me view code also.then I can understand clieally.and also it is not set in to flash message.what about flash message

Hi

Flash message should be used for displaying only one error or one success or …

Below code will take care for displaying error messages …when there are more error in model

<?php echo $form->errorSummary($model); ?>

Have you tried this?

of course I tried with it.bt it gives the msg like this


<!DOCTYPE html PUBLIC

	"-//W3C//DTD XHTML 1.0 Transitional//EN"

	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<title>PHP warning</title>


<style type="text/css">

/*<![CDATA[*/

html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent;margin:0;padding:0;}

body{line-height:1;}

ol,ul{list-style:none;}

blockquote,q{quotes:none;}

blockquote:before,blockquote:after,q:before,q:after{content:none;}

:focus{outline:0;}

ins{text-decoration:none;}

del{text-decoration:line-through;}

table{border-collapse:collapse;border-spacing:0;}


body {

	font: normal 9pt "Verdana";

	color: #000;

	background: #fff;

}


h1 {

	font: normal 18pt "Verdana";

	color: #f00;

	margin-bottom: .5em;

}


h2 {

	font: normal 14pt "Verdana";

	color: #800000;

	margin-bottom: .5em;

}


h3 {

	font: bold 11pt "Verdana";

}


pre {

	font: normal 11pt Menlo, Consolas, "Lucida Console", Monospace;

}......

Do you have any Idea what happned…

this is my contrallor


public function actionUpdategeneral() {

		//$usermodel=new Users;

	$usermodel = Users::model()->findByPk( Yii::app ()->user->id );

		// $usermodel= Users::model()->find('id=:id',array(':id'=>Yii::app()->user->id));

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

		//print_r($_POST['Users']);

		//die();

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

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

			if($usermodel->validate()){

			if($usermodel->update()){

				Yii::app()->user->setFlash('success', 'Your Details Have been Updated Successfully.');

				}	

			}else{

				//$usermodel->getErrors();

// 				$errors=array('First Name Cannot be Empty'.'Last Name Cannot be Empty'.'Prefered Name Cannot be Empty'.'Date Of Birth Cannot be Empty');	

			//	return $usermodel->getErrors();

				//foreach($usermodel->getErrors() as $errors){

				$errors = array();

				foreach ($usermodel->getErrors() as $attribute=>$attributeErrors)

				foreach ($attributeErrors as $error)

							$errors[] = array(

							'attribute'=>$attribute,

							'label'=>$usermodel->getAttributeLabel($attribute), 							'error'=>$error

 							);

 							$response = compact('errors');

							}

 				header('Content-type: application/json');

 				echo CJSON::encode($response);

		

		//Yii::app()->user->setFlash('error',$response);


			}

		$this->redirect (array('index','users'=>$usermodel));

		}

	

[color="#000088"]Hi [/color][color="#000088"]i want to rewrite your method just try this…[/color]

[color="#000088"]function[/color][color="#000000"] actionUpdategeneral[/color]color="#666600"[/color] [color="#666600"]{[/color][color="#000000"]

    &#036;usermodel [/color][color=&quot;#666600&quot;]=[/color] [color=&quot;#660066&quot;]Users[/color][color=&quot;#666600&quot;]::[/color][color=&quot;#000000&quot;]model[/color][color=&quot;#666600&quot;]()-&gt;[/color][color=&quot;#000000&quot;]findByPk[/color][color=&quot;#666600&quot;]([/color] [color=&quot;#660066&quot;]Yii[/color][color=&quot;#666600&quot;]::[/color][color=&quot;#000000&quot;]app [/color][color=&quot;#666600&quot;]()-&gt;[/color][color=&quot;#000000&quot;]user[/color][color=&quot;#666600&quot;]-&gt;[/color][color=&quot;#000000&quot;]id [/color][color=&quot;#666600&quot;]);[/color]  

[color="#000088"] if[/color] [color="#666600"]([/color][color="#000000"]isset [/color]color="#666600")[/color]

[color="#666600"] {[/color][color="#000000"]

               	&#036;usermodel[/color][color=&quot;#666600&quot;]-&gt;[/color][color=&quot;#000000&quot;]attributes [/color][color=&quot;#666600&quot;]=[/color][color=&quot;#000000&quot;] &#036;_POST[/color][color=&quot;#666600&quot;][[/color][color=&quot;#008800&quot;]'Users'[/color][color=&quot;#666600&quot;]];[/color] [color=&quot;#000088&quot;]if[/color][color=&quot;#666600&quot;]([/color][color=&quot;#000000&quot;]&#036;usermodel[/color][color=&quot;#666600&quot;]-&gt;[/color][color=&quot;#000000&quot;]validate[/color][color=&quot;#666600&quot;]())[/color]

[color="#666600"] {[/color][color="#000000"]

               		    [/color][color=&quot;#000088&quot;]if[/color][color=&quot;#666600&quot;]([/color][color=&quot;#000000&quot;]&#036;usermodel[/color][color=&quot;#666600&quot;]-&gt;[/color][color=&quot;#000000&quot;]update[/color][color=&quot;#666600&quot;]())[/color]

[color="#666600"] {[/color][color="#000000"]

                      		    [/color][color=&quot;#660066&quot;]Yii[/color][color=&quot;#666600&quot;]::[/color][color=&quot;#000000&quot;]app[/color][color=&quot;#666600&quot;]()-&gt;[/color][color=&quot;#000000&quot;]user[/color][color=&quot;#666600&quot;]-&gt;[/color][color=&quot;#000000&quot;]setFlash[/color][color=&quot;#666600&quot;]([/color][color=&quot;#008800&quot;]'success'[/color][color=&quot;#666600&quot;],[/color] [color=&quot;#008800&quot;]'Your Details Have been Updated Successfully.'[/color][color=&quot;#666600&quot;]);[/color]										&#036;this[color=&quot;#666600&quot;]-&gt;[/color]redirect [color=&quot;#666600&quot;]([/color]array[color=&quot;#666600&quot;]([/color][color=&quot;#008800&quot;]'index'[/color][color=&quot;#666600&quot;],[/color][color=&quot;#008800&quot;]'users'[/color][color=&quot;#666600&quot;]=&gt;[/color]&#036;usermodel[color=&quot;#666600&quot;]));// when success forward to index[/color]                   			    

[color="#666600"] }[/color] [color="#666600"]}[/color]

[color="#666600"]}[/color]

&#036;this[color=&quot;#666600&quot;]-&gt;[/color]redirect [color=&quot;#666600&quot;]([/color]array[color=&quot;#666600&quot;]([/color][color=&quot;#008800&quot;]'update'[/color][color=&quot;#666600&quot;],[/color][color=&quot;#008800&quot;]'users'[/color][color=&quot;#666600&quot;]=&gt;[/color]&#036;usermodel[color=&quot;#666600&quot;])); // when failure forward to update[/color]

[color="#666600"]}[/color]

[color="#666600"]This should work perfect …try this… cheers[/color]

what do you mean by thi code

is it update page


$this->redirect (array('update','users'=>$usermodel)); // when failure forward to update

in my application ther’s no update page.

Hi,

ya you are right… that is update page…

when failure occurs… you want to retain in same page.

so you have to give the page name where you trigger this request…

I hope it will works

it will give this error


urlencode() expects parameter 1 to be string, object given 

nw This is totally confused to me .pls help me.still it give not errors when put blank to required field.How do I overcome this problem.

I raelly appreciate your help

thanx lot.help me

and


echo $form->errorSummary ( $model );

how to use this code to display errors.can u explain me with sample code.I mean how to code in view and the controler.

thanx

Hi,

In your case you have to use it like this…

echo $form->errorSummary ( $usermodel );

You have never shown me your view code…

Controller code is perfect… u dont need to change controller code except forwarding page when failure occurs…

In which line it is giving this error

[color=#000000]urlencode[/color]color=#666600[/color][color=#000000] expects parameter [/color][color=#006666]1[/color][color=#000000] to be [/color][color=#000088]string[/color][color=#666600],[/color][color=#000000] [/color][color=#000088]object[/color][color=#000000] given[/color]

this is my view code


<?php echo $form->errorSummary($users); ?>

                   <?php  //echo CHtml::errorSummary( $usermodel->getErrors() );?>

                   

                    <?php

                     foreach(Yii::app()->user->getFlashes() as $key => $message) {

	      			 echo '<div id="display-success">' . $message . "</div>\n";

	  					  }

					?>

                    <fieldset class="left">

						<div class="">

                            <?php echo $form->labelEx($users, 'first_name'); ?>

                            <div class="block">

                                <?php echo $form->textField($users, 'first_name', array('style' => 'width:261px;')); ?>

                            </div>

                            <?php echo $form->error($users, 'first_name'); ?>

                        </div>

						

						<div class="">

                            <?php echo $form->labelEx($users, 'gender'); ?>

                          

                            <?php echo ZHtml::enumDropDownList( $users,'gender' ,array('class' => 'dropDownBox')); ?>

                                <?php //echo $form->textField($users, 'gender', array('style' => 'width:261px;')); ?>

                           

                            <?php echo $form->error($users, 'gender'); ?>

                        </div>

                        <div class="">

                            <?php echo $form->labelEx($users, 'date_of_birth'); ?>

                            <div class="block">

                                <?php echo $form->textField($users, 'date_of_birth', array('style' => 'width:261px;')); ?>

                            </div>

                            <?php echo $form->error($users, 'date_of_birth'); ?>

                        </div>

					</fieldset>

					<fieldset class="right">

						<div class="">

                            <?php echo $form->labelEx($users, 'last_name'); ?>

                            <div class="block">

                                <?php echo $form->textField($users, 'last_name', array('style' => 'width:261px;')); ?>

                            </div>

                            <?php echo $form->error($users, 'last_name'); ?>

                        </div>

						<div class="">

                            <?php echo $form->labelEx($users, 'preferred_name'); ?>

                            <div class="block">

                                <?php echo $form->textField($users, 'preferred_name', array('style' => 'width:261px;')); ?>

                            </div>

                            <?php echo $form->error($users, 'preferred_name'); ?>

                        </div>

						


					</fieldset>

					<fieldset style="clear: both;">

						<div class="">

                            <?php echo CHtml::submitButton($users->isNewRecord ? 'Update' : 'Update'); ?>

                        </div>

					</fieldset>   

                    <?php $this->endWidget(); ?>


                </div>

Hi,

Just try with one field…

<?php

foreach(Yii::app()->user->getFlashes() as $key => $message) {

echo '&lt;div class=&quot;flash-' . &#036;key . '&quot;&gt;' . &#036;message . &quot;&lt;/div&gt;&#092;n&quot;;

}

Yii::app()->clientScript->registerScript(

'myHideEffect',


'&#036;(&quot;.flash-success&quot;).animate({opacity: 1.0}, 1000).fadeOut(&quot;slow&quot;);',


CClientScript::POS_READY

);

?>

<div class="form">

<?php $form=$this->beginWidget(‘CActiveForm’, array(

'id'=&gt;'activeform',


'enableAjaxValidation'=&gt;false,

)); ?>

&lt;?php echo &#036;form-&gt;errorSummary(&#036;users); ?&gt;

…other fields should go

&lt;div id=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'pgm_manager'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'pgm_manager',array('size'=&gt;60,'maxlength'=&gt;80,'disabled'=&gt;&#036;action==='view')); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'pgm_manager'); ?&gt;


&lt;/div&gt;

…other fields should go

[color=#666600]<?[/color][color=#000000]php echo [/color][color=#660066]CHtml[/color][color=#666600]::[/color][color=#000000]submitButton[/color][color=#666600]([/color][color=#000000]$users[/color][color=#666600]->[/color][color=#000000]isNewRecord [/color][color=#666600]?[/color][color=#000000] [/color][color=#008800]‘Update’[/color][color=#000000] [/color][color=#666600]:[/color][color=#000000] [/color][color=#008800]‘Update’[/color][color=#666600]);[/color][color=#000000] [/color][color=#666600]?>[/color][color=#000000]

[/color]

<?php $this->endWidget(); ?>

</div><!-- form -->