Validation Problem

I am using a single form for more than one model as illustrated…

… However I am using three models.

The ServiceRequest input for activeListBox selection prompts an Error: "The Service Selection cannot be blank.", even though the selections are showing as selected in the side column via a javascript method.

I wasn’t sure what to do with the third validation since the example was for two models. Does the Ajax function (at the bottom) need to be corrected?

	public function actionCreate()


		$this->layout = '//layouts/column1';

		$ServiceRequest=new ServiceRequest;

		$VisitorInfo=new VisitorInfo;

		$ServiceItem=new ServiceItem;





			//populate input data to $a and $b





			//validate both $a and $b


			$valid=$VisitorInfo->validate() ;

			$valid=$ServiceItem->validate() && $valid;




				// use false parameter to disable validation




Yii::app()->user->setFlash('serviceRequest','Thank you! Your request has been submitted. Please check your email for confirmation. We will contact you as soon as possible.');




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




	 * Performs the AJAX validation.

	 * @param CModel the model to be validated


	protected function performAjaxValidation($models)


		if(isset($_POST['ajax']) && $_POST['ajax']==='service-request-form')


			echo CActiveForm::validate($models);




Not sure if it solves your problem but this code is not good:


   $valid=$VisitorInfo->validate() ;

   $valid=$ServiceItem->validate() && $valid;

If there are errors in the first validation $valid is set to false, but then is replaced by the value of the second validation

The second line should be

$valid=$VisitorInfo->validate() && valid;

I am also not sure if it is a good practice but you can manage the validation of all models’ attributes into the first model only. I’ve done it and it works but not sure if it is the best way.

This didn’t fix the problem… Although it is nice to have it set properly.

//validate both $a and $b


			$valid=$VisitorInfo->validate() && $valid;

			$valid=$ServiceItem->validate() ;



The rules for each of the models looked properly set … So why would the field act as if it hadn’t been selected when it obviously had been selected as the javascript illustrated by displaying the items in the side column … and that’s what the problem turned out to be.

When I changed the javascript div ID, which holds the selectionbox, from #ServiceRequest_selection to #ServiceRequest_select then the form acted correctly!

<?php Yii::app()->clientScript->registerScript(

      '#item_selection_box', '

   $("#ServiceRequest_select").change(function() {

      var content = $("#ServiceRequest_select option:selected").text();


   }); ');



Problem is that now you removed the "& valid" from the 3rd line… this way if there are validation errors in the first 2 models the value get lost as in the $valid variable you get only the result of the validation of the 3rd module.

And just now I noticed the line



Do you understand the difference of that line and


let’s see mdomba,…

I read, re-read and keep trying. What I gained from my study so far, is that the $ indicates a placeholder for a value.

So, the value of ($valid) in this context is found within the ->validate(function) as it compares the input against the model rules. The value, as expressed by rule conditions being met, or not, is either pass or no pass.

In comparison, (‘valid’) would imply a reference to a file or attribute or something other than a variable, which in the context of this application doesn’t make sense.

Can you correct or add anything to further my understanding? Your question really helped me to think it through.

To be honest, I don’t know why is was like that, I know that Gii didn’t produce it that way and I can’t imagine why I would have changed it, but there it was.


	//validate both $a and $b


	$valid=$VisitorInfo->validate() && $valid;

	$valid=$ServiceItem->validate() && $valid;



Now I get to figure out how to correct the rest of that controller. I do not want a redirect after save, I just want a refresh with a confirmation notice. I’ll be sure to holler if it gives me too much trouble!

You really need to read a bit about PHP basics!


is a string


is a variable that holds some value


as ‘valid’ is a string this is always true


this is completely different - it checks if the value of the $valid variable is true or false

Reading is good. The PHP manuel offers a lot of examples, as do other sources and I refer to them. However for me reading isn’t enough, it is through applying, trying and fixing with qualified guidence that I find an understanding. I have been to tutorial sites and followed along as well.

Is there a really big difference between a php site tutorial and the Yii site?

Of course and the biggest difference is that the Yii project is actually producing something usefull for me. At the same time, it pulls together the MVC, OOP and framework. OMGosh, it’s all in this one place, that is awesome!

If I were persuing a career in php then I would go to school, but my motivation to learn is of a personal desire to understand how my site works. There is a control panel at my hosting which will produce forms for me but it doesn’t teach me anything. I like to learn, even if I am slow at it sometimes. I find it very rewarding to learn and to create useful things.

Obviously this forum is supported by very talented and knowedgable people, and I find it a privilege to be able to feed off of the experience found here. I am truely grateful for the generous assistance and guidence of the members.

I agree with you on experience, but as you now understand your code mistake… possibly you will understand that you cannot learn writing until you learn the alphabet. And if you want to understand how your site is working, then IMO it’s a MUST that you first learn the PHP basics and with that I mean “the basics”.

Just a big note here - I wrote you that you need to read (a better word is learn) about PHP basics… not tutorials about PHP sites !

I think that having the desire to learn is the first step to succeeding. :)

Check this link:

Thanks for the link jimlam, … I am finding it to be very intresting!