Validation Problem

I am using a single form for more than one model as illustrated… http://www.yiiframework.com/wiki/218/how-to-use-single-form-to-collect-data-for-two-or-more-models-cactiveform-and-ajax-validation-edition

… 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;

		

		$this->performAjaxValidation(array($ServiceRequest,$VisitorInfo,$ServiceItem));


		if(isset($_POST['ServiceRequest'],$_POST['VisitorInfo'],$_POST['ServiceItem']))

		{

			//populate input data to $a and $b

			$ServiceRequest->attributes=$_POST['ServiceRequest'];

			$VisitorInfo->attributes=$_POST['VisitorInfo'];

			$ServiceItem->attributes=$_POST['ServiceItem'];

			

			//validate both $a and $b

			$valid=$ServiceRequest->validate();

			$valid=$VisitorInfo->validate() ;

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

			

			if('valid')


			{

				// use false parameter to disable validation

				$ServiceRequest->save(false);

				$VisitorInfo->save(false);

				$ServiceItem->save(false);




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->refresh();

			}

		}


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

			'ServiceRequest'=>$ServiceRequest,'VisitorInfo'=>$VisitorInfo,'ServiceItem'=>$ServiceItem));

	}


	/**

	 * 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);

			Yii::app()->end();

		}

	}

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




   $valid=$ServiceRequest->validate();

   $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=$ServiceRequest->validate();

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

			$valid=$ServiceItem->validate() ;

			

			if('valid')

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();

      $("#item_selection_box").text(content);

   }); ');


?>

Thanks!

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


if('valid')

???

Do you understand the difference of that line and


if($valid)

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.

Correction…


	//validate both $a and $b

	$valid=$ServiceRequest->validate();

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

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

			

	if('$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!


'valid'

is a string


$valid

is a variable that holds some value


if('valid')

as ‘valid’ is a string this is always true


if($valid)

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:

http://www.tuxradar.com/practicalphp

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