I am using renderParial() function to render views->subscriber->_form from views->layouts->main.php Problem i am getting is I am failed to insert data using this. Gii generated CRUD is working perfectly but when i render _form and want to insert data in database it's not working. Here is my code. main.php
<div class="col-lg-6" style="text-align:right;">
<span>Subscribe Newsletter</span>
<div style="float:right;margin-left:10px;">
<?php
$model=new Subscription();
$this->renderPartial("/subscription/_form",array('model'=>$model));?>
</div>
and _form.php
<?php
/* @var $this SubscriptionController */
/* @var $model Subscription */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'subscription-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>false,
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->textField($model,'email',array('size'=>30,'maxlength'=>30,'placeholder'=>'Email Address')); ?>
<?php echo $form->error($model,'email'); ?>
<?php echo CHtml::submitButton($model->isNewRecord ? 'Subscribe' : 'Save',array('class'=>'btn btn-xs btn-success subscribeBtn')); ?>
</div>
<?php $this->endWidget(); ?>
</div>
As adamS and Michiel have commented, if you want to put a form or data in your main.php layout file, you should use a widget.
To create a widget, you need to do the following:
1: Create a php file in your /protected/components/
dir, something like SubscriptionWidget.php
2: Create a dir views
in your components dir
3: Create your view .php file in your /protected/components/views/
, something like subscriptionWidget.php
4: Put the following code in your SubscriptionWidget.php
file:
<?php
class SubscriptionWidget extends CWidget
{
public function init()
{
}
public function run()
{
$model = new SubscriptionForm;
if(isset($_POST['SubscriptionForm']))
{
// proces the data
}
$this->render('subscriptionWidget', array('model'=>$model));
}
}
?>
Your widget is done. All you need to do now is call it in your main.php
layout file, like so:
<!doctype html>
<html lang="en">
...
<?php $this->widget('SubscriptionWidget'); ?>
...
</html>
Also, don't forget to put the form in your newly created view file.
Hope this helps.