display validation errors

Hi,

this is a newbee question.

I’m trying to make a simple app that would just collect data from a form, validate the input, concatenate it if it’s right and display either the concatenated data back or the errors. The rendering of either the errors or the concatenated string should be on the same page where the data is collected.

So far I managed to validate the input, concatenate it and display it.

I’m stucked in displaying the errors. I’d like to display errors as yii does on the login form. I can get the errors using $model->getErrors() on my controller but I don’t seem to be able to render them.

I’ll post the function to concatenate:




public function actionConcatenate()

	{

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

			{

				$model = new CollectData();

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

				

				if($model->validate())

				{

					$model->resultado = implode(', ', $model->attributes);

					$this->render('index', array ('model' => $model));

				}

				else {

					$errores = $model->getErrors();

					$this->render('index', array('model' => $errores));

				}

				

			}

	}



I’d appreciate if you could drive me in the right direction.

Thanks

Posting your view code would help…

Anyway you can use errorSummary() to display the errors - http://www.yiiframework.com/doc/api/1.1/CActiveForm#errorSummary-detail

Thanks mdomba.

Here’s the view code:




<?php

$this->breadcrumbs=array(

	'Calculator',

);?>

<h1><?php echo $this->id . '/' . $this->action->id; ?></h1>


<div class='form'>


<?php


$model = (isset($model))? $model : new CollectData(); ?>


<?php


if (! empty($model->resultado))

{

	echo "<p>";

	echo "Las palabras concatenadas son: ".$model->resultado;

	echo "</p>";

}

?>


<?php echo CHtml::beginForm('index.php?r=calculator/concatenate', 'post'); ?>


<?php echo CHtml::errorSummary($model); ?>


<div class="row">

<?php echo CHtml::activeLabel($model,'palabra'); ?>

<?php echo CHtml::activeTextField($model,'palabra', array('value'=>'')); ?>

</div>


<div class="row">

<?php echo CHtml::activeLabel($model,'palabra5'); ?>

<?php echo CHtml::activeTextField($model,'palabra5', array('value'=>'')); ?>

</div>


<div class="row">

<?php echo CHtml::activeLabel($model,'numero'); ?>

<?php echo CHtml::activeTextField($model,'numero', array('value'=>'')); ?>

</div>


<?php echo CHtml::submitButton('Submit'); ?>





<?php echo CHtml::endForm(); ?>




</div>




Oh!

I forgot to mention. All I get when inputing wrong data is a fatal error saying:


Fatal error: Call to a member function getErrors() on a non-object in /opt/lampp/frameworks/yii-1.1.5.r2654/framework/web/helpers/CHtml.php on line 1563

You are using the variable $model for the actual model data and for the concatenated results and errors…

You can use some other variables for the concatenaded values that you can check in the view if they are set like:




if($model->validate())

{

   $this->render('index', array('results' => implode(', ', $model->attributes)));

}

else {

   $this->render('index', array('errors' => $model->getErrors()));

}



Hi, thanks for the reply.

I’m affraid it still says I’m calling getErrors() on a non-object.

You need to check the value of $model just before calling $model->getErrors()… seems it’s not an object…

try this:




public function actionConcatenate()

        {

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

                        {

                                $model = new CollectData();

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

                                

                                if($model->validate())

                                {

                                        $model->resultado = implode(', ', $model->attributes);

                                }

                                $this->render('index', array ('model' => $model));                                

                        }

        }



do not change your view code, just try this, it’s must work.

OK, thank you.

It’s fixed now (although not in an elegant way). I’ll try to post the code later for the records.

Oh! I didn’t see your code. I’ll try that later.

Hi,

thank you both for your answers; I learned from you.

@maschingan, this is exactly what I was looking for.

Let me now sum up what I learned and see if I’m right:

  1. on the controller, $model->validate() validates the data and, very important, sets $model->errors

  2. on the view file, CHtml::errorSummary($model) handles the whole error report: reads the $model->errors, writes the proper html.

I’d appreciate if you can check if there is anything wrong on what I’ve just said.

In any case, I thank you both.

yes, you right. Some advice:

You can write




public function actionConcatenate()

        {

                        $model = new CollectData();

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

                        {

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

                                

                                if($model->validate())

                                {

                                        $model->resultado = implode(', ', $model->attributes);

                                }

                        }

                         $this->render('index', array ('model' => $model));

        }



and in view remove some lines




<?php

$this->breadcrumbs=array(

        'Calculator',

);?>

<h1><?php echo $this->id . '/' . $this->action->id; ?></h1>


<div class='form'>


<?php


if (! empty($model->resultado))

{

        echo "<p>";

        echo "Las palabras concatenadas son: ".$model->resultado;

        echo "</p>";

}

?>


<?php echo CHtml::beginForm('', 'post'); ?>


<?php echo CHtml::errorSummary($model); ?>


<div class="row">

<?php echo CHtml::activeLabel($model,'palabra'); ?>

<?php echo CHtml::activeTextField($model,'palabra', array('value'=>'')); ?>

</div>


<div class="row">

<?php echo CHtml::activeLabel($model,'palabra5'); ?>

<?php echo CHtml::activeTextField($model,'palabra5', array('value'=>'')); ?>

</div>


<div class="row">

<?php echo CHtml::activeLabel($model,'numero'); ?>

<?php echo CHtml::activeTextField($model,'numero', array('value'=>'')); ?>

</div>


<?php echo CHtml::submitButton('Submit'); ?>





<?php echo CHtml::endForm(); ?>


</div>



and you will be not need two actions in controller (i guess you have two actions for this view).

Yes I had two actions for this view.

I think I get what you mean: put the most part of the logic on the controller.

Thanks

Thank you so much! Was my problem too.

hey guys i have same error

this is my controller

case Yii::app()->par->PAGE_ADD_PRESENTATION :

			if (&#33;Yii::app()-&gt;user-&gt;id) &#036;this-&gt;redirect(array('contenu/'.Yii::app()-&gt;par-&gt;PAGE_PERSONNEl));


		


			&#036;view = &quot;_contenu_add_presentation&quot;;





			// on choisit le template n°3


			&#036;this-&gt;layout='ocelles5';





			// on regarde si on propose un nom de presentation ou une place dans l'arborescence


			&#036;place = Yii::app()-&gt;request-&gt;getQuery('place',NULL);


			&#036;nom = Yii::app()-&gt;request-&gt;getQuery('nom',NULL);





			// on charge la palette


			&#036;this-&gt;palette=&#036;this-&gt;loadPalette(&#036;this-&gt;idconcept);





			// registrer le javascript de gestion d'ajout de presentation


			&#036;cs-&gt;registerScriptFile(Yii::app()-&gt;baseUrl.'/js/concept_setup.js',CClientScript::POS_HEAD);


			// on va utiliser le script jsTree


			&#036;cs-&gt;registerScriptFile(&#036;baseUrl.'/js/jquery.jstree.js',CClientScript::POS_HEAD);


			// registrer jqueryui


			&#036;cs-&gt;registerCoreScript('jquery.ui');


			// registrer le css (on doit le charger avant d'appeler une éventuelle boite de dialogue)


			&#036;cs-&gt;registerCssFile(Yii::app()-&gt;getBaseUrl(true) . &quot;/&quot; . Yii::app()-&gt;par-&gt;PATH_THEME_JQUERYUI . &quot;/&quot; . &#036;this-&gt;palette . &quot;/&quot; . &#036;this-&gt;palette . &quot;.css&quot;);





			// on va utiliser le script jCarousel pour afficher la liste des signes du presentation


			&#036;cs-&gt;registerScriptFile(&#036;baseUrl.'/js/jquery.jcarousel.min.js');


			&#036;cs-&gt;registerScriptFile(&#036;baseUrl.'/js/jcarousel_setup.js');


			&#036;cs-&gt;registerCssFile(&#036;baseUrl.'/css/jcarousel_skins/ocelles/skin_signes.css'); // on utilise un skin particulier





			// on charge le titre de la page


			&#036;this-&gt;pageTitle=&#036;this-&gt;loadPageTitle(&#036;this-&gt;idconcept);





				


			// ------------


			// ZONE BANNER


			// ------------


			// on charge la nuance pour la zone banner


			&#036;this-&gt;banner_nuance=&#036;this-&gt;loadBannerNuance(&#036;this-&gt;idconcept);





			// on charge le contenu de la zone banner : pour la page presentation il s'agira de la vue banner_presentation


			&#036;this-&gt;banner_content = array(


					&quot;type&quot;=&gt;&quot;default&quot;,


					&quot;content&quot;=&gt;&#036;this-&gt;renderPartial('_view_banner_add_presentation',array(


							&quot;place&quot;=&gt;&#036;place,


							&quot;nom&quot;=&gt;&#036;nom


					),true,false)


			);





			// ----------------


			// ZONE PRINCIPALE


			// ----------------


			// on charge la nuance de la palette


			&#036;this-&gt;content_nuance = &#036;this-&gt;loadNuance(&#036;this-&gt;idconcept);





			&#036;this-&gt;render(&#036;view,array(


					&quot;place&quot;=&gt;&#036;place,


					&quot;nom&quot;=&gt;&#036;nom


			));





			break;

this is view file

<?php

// VUE CONCEPT => contenu de la page concept

?>

<div class="concept_main_action first <?php echo $this->palette;?> n3">

&lt;?php if (Yii::app()-&gt;user-&gt;id){

$link = array(‘contenu/’.Yii::app()->par->PAGE_ADD_PRESENTATION);

$traduction = Ocelles::getTraduction(Yii::app()->par->PAGE_ADD_PRESENTATION);

&#036;trad = Ocelles::getTraduction(Yii::app()-&gt;par-&gt;INTERFACE_PRESENTATION_CREER);


MyFunctions::displayTraduction(&#036;trad[&quot;traductionid&quot;],&quot;traduction survol_video&quot;);


echo CHtml::link(&quot;Créer une présentation&quot;,array('contenu/view','id'=&gt;Yii::app()-&gt;par-&gt;PAGE_ADD_PRESENTATION),array(&quot;title&quot;=&gt;&quot;Créer une présentation&quot;));


} else {


	&#036;link = array('contenu/'.Yii::app()-&gt;par-&gt;PAGE_PERSONNEL);


	&#036;traduction = Ocelles::getTraduction(Yii::app()-&gt;par-&gt;PAGE_PERSONNEL);


	&#036;trad = Ocelles::getTraduction(Yii::app()-&gt;par-&gt;INTERFACE_PRESENTATION_CREER);	


	MyFunctions::displayTraduction(&#036;trad[&quot;traductionid&quot;],&quot;traduction survol_video&quot;);


	echo CHtml::link(&quot;Créer une présentation&quot;,array('contenu/view','id'=&gt;Yii::app()-&gt;par-&gt;PAGE_ADD_PRESENTATION),array(&quot;title&quot;=&gt;&quot;Créer une présentation&quot;));


	}


?&gt;

</div>

<div class="concept_main_action_icon first">

&lt;?php echo CHtml::link(Chtml::image(Yii::app()-&gt;baseUrl . &quot;/&quot; . Yii::app()-&gt;par-&gt;PATH_IMAGES . &quot;concept_add_signe.png&quot;,&quot;Créer une présentation&quot;),array('contenu/view','id'=&gt;Yii::app()-&gt;par-&gt;PAGE_ADD_PRESENTATION),array(&quot;title&quot;=&gt;&quot;Créer une présentation&quot;));?&gt;

</div>

<div class="concept_main_action <?php echo $this->palette;?> n3">

&lt;?php 


&#036;trad = Ocelles::getTraduction(Yii::app()-&gt;par-&gt;INTERFACE_DERNIERES_PRESENTATIONS);


MyFunctions::displayTraduction(&#036;trad[&quot;traductionid&quot;],&quot;traduction survol_video&quot;);


echo CHtml::link(&quot;Dernières présentation ajoutées&quot;,array(''),array(&quot;title&quot;=&gt;&quot;Dernières présentation ajoutées&quot;,&quot;onclick&quot;=&gt;&quot;js:&#036;('#dernieres_pages').slideToggle('slow');return false;&quot;));


?&gt;

</div>

<div class="concept_main_action_icon">

&lt;?php echo CHtml::link(Chtml::image(Yii::app()-&gt;baseUrl . &quot;/&quot; . Yii::app()-&gt;par-&gt;PATH_IMAGES . &quot;concept_add_signe.png&quot;,&quot;Dernières présentation ajoutées&quot;),array(''),array(&quot;title&quot;=&gt;&quot;Dernières présentation ajoutées&quot;,&quot;onclick&quot;=&gt;&quot;js:&#036;('#dernieres_pages').slideToggle('slow');return false;&quot;));?&gt;

</div>

<div id="dernieres_pages">

&lt;?php


&#036;limite = 0;


&#036;concepts = Ocelles::getConcepts(Yii::app()-&gt;user-&gt;id);


foreach (&#036;concepts as &#036;key=&gt;&#036;concept) {


	&#036;limite++;


	if (&#036;limite == 9) break;


	&#036;icone = Ocelles::getIcone(&#036;key,Yii::app()-&gt;user-&gt;id);


	&#036;traduction = Ocelles::getTraduction(&#036;key,Yii::app()-&gt;user-&gt;id);


	&#036;palette = Ocelles::loadPalette(&#036;key,Yii::app()-&gt;user-&gt;id);


	?&gt;


	&lt;div class=&quot;proche &lt;?php echo &#036;palette;?&gt; n2&quot;&gt;


		&lt;div style=&quot;float:left;width:30px;height:20px;&quot;&gt;


		&lt;?php if (&#036;icone) MyFunctions::displayIcone(&#036;key.&quot;/&quot;.Yii::app()-&gt;par-&gt;TN4_PREFIX.&#036;icone,&#036;concept,&#036;concept);?&gt;


		&lt;/div&gt;


		&lt;?php if (&#036;traduction) MyFunctions::displayTraduction(&#036;traduction[&quot;traductionid&quot;],&quot;traduction survol_video&quot;);?&gt;


		&lt;?php echo CHtml::link(&#036;concept,array('contenu/view','id'=&gt;&#036;key),array('id'=&gt;&#036;key,&quot;title&quot;=&gt;&#036;concept));?&gt;


	&lt;/div&gt;


&lt;?php


}


?&gt;

</div>

and error is

Erreur 500

Trying to get property of non-object

i don’t know why it showing this error and also it didn’t show path for error.