A Defined Variable Becomes Undefined Variable?

Why would a variable work in the Create action but not the Update? Why would a working variable that hadn’t been changed just stop working? I have been over and over the code, I have searched for answers … I really could use some help, Please!

I was Creating and Updating, all of the emails were working, perfectly! Then I changed from registration required to using the captcha feature. I find it odd that the problem is with a mailer variable which didn’t get changed and even odder … it is only an error for the Update action, even though the same variable still works for Create.

The only thing I had to change for the mailer was where the email address for the visitor came from, ($model->email rather than $user->email).

PHP Error

Undefined variable: message

Controller.php (218)

$mailer->Body = $message; // line in actionUpdate

First; Controller Update code causing error - the working Create code is posted below for comparison. Any insight into the problem would be greatly appreciated.


	public function actionUpdate($id)

	{

		$model=$this->loadModel($id);


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

			if($model->save())


$message = strtr ('Updated Referral Submitted


Changes have been made to the referral database.

A confirmation has been sent to the visitor email address.


ID#: {id}

Date/Time: {date}

Updated by: {byFName} {byLName}


Referral Name: {leadFName} {leadLName}

Referral Phone: {call}

Comments: {message}', array(

'{id}' => $model->id,

'{date}' => $model->date,

'{byFName}' => $model->customerFirstName,

'{byLName}' => $model->customerLastName,

'{leadFName}' => $model->firstName,

'{leadLName}'=> $model->lastName,

'{call}' => $model->phone,

'{message}' => $model->comments));


$title = strtr ('Updated Referral #{id} by {byFName} {byLName}',array(

'{id}' => $model->id,

'{byFName}' => $model->customerFirstName,

'{byLName}' => $model->customerLastName,

));


$mailer = Yii::createComponent('application.extensions.mailer.EMailer');

$mailer->Host = 'localhost';

$mailer->IsSMTP();

$mailer->From = Yii::app()->params['adminEmail'];

$mailer->AddReplyTo('No-Reply@mysite.net');

$mailer->AddAddress(Yii::app()->params['adminEmail']);

$mailer->AddCC(Yii::app()->params['adminOtherEmail']);

$mailer->FromName = 'Update Referral';

$mailer->CharSet = 'UTF-8';

$mailer->Subject = $title;

$mailer->Body = $message; // <------------------THIS IS THE UNDEFINED VARIABLE ERROR

$mailer->Send();


//send thank-you/copy of update to visitor

$message = strtr ('Thank-you {byFName} {byLName}, your updated referral  submission at {siteAddress} has been received.


ID#: {id}

Submitted: {date}

Referral Name: {leadFName} {leadLName}

Referral Phone: {call}

Comments: {message}


Kindest Regards,

{siteAdmin}', array(

'{byFName}' => $model->customerFirstName,

'{byLName}' => $model->customerLastName,

'{siteAddress}' => 'http://mysite.net',

'{siteAdmin}' => Yii::app()->params['siteAdmin'],

'{id}' => $model->id,

'{date}' => $model->date,

'{leadFName}' => $model->firstName,

'{leadLName}'=> $model->lastName,

'{call}' => $model->phone,

'{message}' => $model->comments));

$mailer = Yii::createComponent('application.extensions.mailer.EMailer');

$mailer->Host = 'localhost';

$mailer->IsSMTP();

$mailer->From = Yii::app()->params['adminEmail'];

$mailer->AddReplyTo('No-Reply@mysite.net');

$mailer->AddAddress($model->email); // <-------------------THIS IS WHAT GOT CHANGED

$mailer->FromName = 'Site Admin';

$mailer->CharSet = 'UTF-8';

$mailer->Subject = 'Updated Referral Submission Received';

$mailer->Body = $message;

$mailer->Send();




				$this->redirect(array('view','id'=>$model->id));

		}


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

			'model'=>$model,

		));

	}

Controller Create Works


	public function actionCreate()

	{

		$model=new Referral;


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

			if($model->save())

$message = strtr ('New Referral Submitted


A thank-you email has been sent for supplying the following information:


ID#: {id}

Submitted by: {byFName} {byLName}

{date}


Referral Name: {leadFName} {leadLName}

Referral Phone: {call}

Comments: {message}', array(

'{id}' => $model->id,

'{date}' => $model->date,

'{byFName}' => $model->customerFirstName,

'{byLName}' => $model->customerLastName,

'{leadFName}' => $model->firstName,

'{leadLName}'=> $model->lastName,

'{call}' => $model->phone,

'{message}' => $model->comments));

$title = strtr ('New Referral #{id} by {byFName} {byLName}',array(

'{id}' => $model->id,

'{byFName}' => $model->customerFirstName,

'{byLName}' => $model->customerLastName,

));


$mailer = Yii::createComponent('application.extensions.mailer.EMailer');

$mailer->Host = 'localhost';

$mailer->IsSMTP();

$mailer->From = Yii::app()->params['adminEmail'];

$mailer->AddReplyTo('No-Reply@mysite.net');

$mailer->AddAddress(Yii::app()->params['adminEmail']);

$mailer->AddCC(Yii::app()->params['adminOtherEmail']);

$mailer->FromName = 'New Referral';

$mailer->CharSet = 'UTF-8';

$mailer->Subject = $title;

$mailer->Body = $message; // <------------------- THIS WORKS and is set the same as Update

$mailer->Send();


//send thank you to visitor

$message = strtr ('Thank-you {byFName} {byLName}, your referral submission has been received at {siteAddress}.


ID#: {id} 

Submitted: {date}

Referral Name: {leadFName} {leadLName}

Referral Phone: {call}

Comments: {message}


Kindest Regards,

{siteAdmin}', array(

'{byFName}' => $model->customerFirstName,

'{byLName}' => $model->customerLastName,

'{siteAddress}' => 'http://mysite.net',

'{siteAdmin}' => Yii::app()->params['siteAdmin'],

'{id}' => $model->id,

'{date}' => $model->date,

'{leadFName}' => $model->firstName,

'{leadLName}'=> $model->lastName,

'{call}' => $model->phone,

'{message}' => $model->comments));


$mailer = Yii::createComponent('application.extensions.mailer.EMailer');

$mailer->Host = 'localhost';

$mailer->IsSMTP();

$mailer->From = Yii::app()->params['adminEmail'];

$mailer->AddReplyTo('No-Reply@mysit.net');

$mailer->AddAddress($model->email);

$mailer->FromName = 'Site Admin';

$mailer->CharSet = 'UTF-8';

$mailer->Subject = 'New Referral Submission Received';

$mailer->Body = $message; // <------------------------ THIS WORKS and is set the same for Update

$mailer->Send();




				$this->redirect(array('view','id'=>$model->id));

		}


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

			'model'=>$model,

		));

	}

Sorry to post so much code, …

Seems like you’re missing ‘{}’ around your code block, so $message becomes defined only if $model->save() returns true, but all the other lines (’$title = …’ and ‘$mailer = …’ get executed every time.

So, then validation fails, you got the ‘undefined’ error.

PS. I mean, it should looks like this:




if ($model->save()) {

    $message = '...';

    $title = '...';

    $mailer = '...';

    $mailer->Send();

}



Angel, if the problem was misplacement of the ‘{}’ then why does the Create code work? They have the same code structure. Why did they both work before? [b]The only controller change I made was where the visitor email address was pulled from.

[/b]

In consideration of failed validation, I checked the model Rules.

I then went to the browser where the php Error message was displayed and went BACK in the browser to view the form with the input data as it was displayed awaiting for update.

All of the form fields were populated with the entry data EXCEPT for the repeatEmail field. It was empty. AhHa, so validation failed because the repeatEmail field was empty which is Reguired according to the rules.

If validation failed due to the empty field then why didn’t the screen just prompt for that to be filled and continue? At that point, I chose to add a repeatEmail field to the database so that field would be populated for the visitor by default.

I tried the process again and got the same error except now it is in the Create action.

PHP Error

Undefined variable: message

Controller.php (117)

$mailer->Body = $message; // line in actionCreate

I am posting the rules however; If it were a Rules problem then wouldn’t a flash error message display instead of a PHP error message? Why would a change in rules cause the Create to now have error? I haven’t made any changes to the Controller because I am certain that the problem isn’t there because I haven’t done any major change to it. The major changes have been made elsewhere.


	public function rules()

	{

		// NOTE: you should only define rules for those attributes that  will receive user inputs.

		return array(

			array('firstName, lastName, customerFirstName, customerLastName, phone, email, repeatEmail, verifyCode', 'required'),

		// emails need to match

			array('email', 'compare', 'compareAttribute'=>'repeatEmail','message'=>'Email needs to match'),

			array('comments', 'safe'),

		// verifyCode needs to be entered correctly

    			array('verifyCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd')),


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

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

			array('id, firstName, lastName, phone, customerFirstName, customerLastName, date, email', 'safe', 'on'=>'search'),

		);

	}

Problem Solved,…

Angel De La Noche, You were right!!! :)

I compared the referralController with the siteController and yes indeed there were missing ‘{}’ just as you pointed out. They must have been inadvertantly deleted. (?? shrug don’t know how/when that happened ??) They were replaced and testing was successful!

Thank-You very much for your assistance!