Cformmodel In Layouts/main.php

Hi all

Suppose I want to place my contact form inside my footer under layouts/main.php, how would I do this? I made a new form Model, included it in my SiteController inside ActionIndex (which I assume controls main.php & Index.php) and used CActiveForm widget but I get Undefined variable models. If I move my form to index.php it works. What’s the problem here?

My model, called it NewsLetter


<?php


/**

 * ContactForm class.

 * ContactForm is the data structure for keeping

 * contact form data. It is used by the 'contact' action of 'SiteController'.

 */

class NewsLetter extends CFormModel

{

	

	public $email;

	


	/**

	 * Declares the validation rules.

	 */

	public function rules()

	{

		return array(

			// name, email, subject and body are required

			array(' email', 'required'),

			

			// email has to be a valid email address

			array('email', 'email'),

			

		);

	}


	/**

	 * Declares customized attribute labels.

	 * If not declared here, an attribute would have a label that is

	 * the same as its name with the first letter in upper case.

	 */

	public function attributeLabels()

	{

		return array(

			'email'=>'Email Address',

		);

	}

}

my SiteController







public function actionIndex()

	{

		// renders the view file 'protected/views/site/index.php'

		// using the default layout 'protected/views/layouts/main.php'

	

	$models=new NewsLetter;

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

		{

			$models->attributes=$_POST['NewsLetter'];

			if($models->validate())

			{

				$headers="From: {$models->email}\r\nReply-To: {$models->email}";

				mail(Yii::app()->params['newsEmail'],$headers, $models->email);

				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');

				$this->refresh();

			}

		}

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





}






inside layouts/main.php





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

	'id'=>'contact-form',

	'enableClientValidation'=>true,

	'clientOptions'=>array(

		'validateOnSubmit'=>true,

	),

)); ?>

        <td><img src="images/join-our-mailing-list.gif" alt=""></td>

        <td>	

		<?php echo $form->labelEx($models,'email'); ?>

		<?php echo $form->textField($models,'email'); ?>

		<?php echo $form->error($models,'email'); ?>

		

        </td>

		

		<div class="row buttons">

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

	</div>


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






Like I said I get undefined variable models. However if I move my form to index.php I get no problems. I named it $models because I already use $model in the same main file.

Thanks

So I have to declare my model in main.php


 $models=new NewsLetter;

. However on Index.php I didn’t need to, either way it’s fine now.

Create a widget for this porpuse.

In the widget code you do:

class contactWidget extends CWidget


public function run()

{

 $models=new NewsLetter;

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

                {

                        $models->attributes=$_POST['NewsLetter'];

                        if($models->validate())

                        {

                                $headers="From: {$models->email}\r\nReply-To: {$models->email}";

                                mail(Yii::app()->params['newsEmail'],$headers, $models->email);

                                Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');

                                $this->refresh();

                        }

                }

                $this->render('contact',array('models'=>$models));

}

Place it in components, and the place the subview contact in components/views:




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

        'id'=>'contact-form',

        'enableClientValidation'=>true,

        'clientOptions'=>array(

                'validateOnSubmit'=>true,

        ),

)); ?>

        <td><img src="images/join-our-mailing-list.gif" alt=""></td>

        <td>    

                <?php echo $form->labelEx($models,'email'); ?>

                <?php echo $form->textField($models,'email'); ?>

                <?php echo $form->error($models,'email'); ?>

                

        </td>

                

                <div class="row buttons">

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

        </div>


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

Thanks Zaccaria, that worked smoothly. Going to be using widgets more often from now.