Hi Zaccaria,
I generated all forms with CRUD. Then I modified a bit. I was aware that it saved automatically after I changed and move the focus to the other page. The process is tested in my Update page only.
This is my Update Action.
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
*/
public function actionUpdate()
{
$model = new TsAddressBook('update');
$model->setIsNewRecord(false);
// If not in login state, redirect to Login page
// if(!TsCustomers::model()->isLoginState())
// $this->redirect(array(‘site/login’));
if(isset($_POST['TsAddressBook']))
{
$_addressbookId = Yii::app()->user->getState('sesAddressBookId');
//$model = $this->retrieveModel($_addressbookId);
$model->attributes=$_POST['TsAddressBook'];
$model->address_book_id = $_addressbookId;
//$model->customers_id= TsCustomers::model()->getLoginUserId();
Yii::trace('Before Validate.....................');
if($model->validate()) {
Yii::trace('After Validate.....................');
//$model->address_book_id = Yii::app()->user->getState('sesAddressBookId');
//$model->customers_id= TsCustomers::model()->getLoginUserId();
//Yii::trace('Address book id for Update.................'.$model->address_book_id);
//Yii::trace('Is New Record:...................'.$model->getIsNewRecord());
if($model->save(false))
$this->redirect(array('view','id'=>$model->address_book_id));
}
else {
$this->render('update',array(
'model'=>$model,
));
}
}
else {
$model = $this->retrieveModel(Yii::app()->user->getState('sesAddressBookId'));
if($model !== null)
{
Yii::app()->user->setState('sesAddressBookId', $model->address_book_id);
$this->render('update',array(
'model'=>$model,
));
}
}
}
This is my Update view page.
<?php $form=$this->beginWidget(‘CActiveForm’, array(
'id'=>'ts-address-book-form',
'enableAjaxValidation'=>false,
)); ?>
<?php $countries = TsCountries::model()->getCountries(); ?>
<script type="text/javascript">
$(document).ready(function(){
$('#butCancel').click(function(){
$('#ts-address-book-form').action = '<?php echo Yii::app()->request->baseUrl;?>/index.php?r=tsAddressBook/view';
$('#ts-address-book-form').submit();
});
$('#_checkBilling').change(function(){
if($('#_checkBilling').is(':checked')) {
$('#billingaddressblock').show('slow');
}
else {
$('#billingaddressblock').hide(1000);
}
});
$('#_checkDelivery').change(function(){
if($('#_checkDelivery').is(':checked')) {
$('#deliveryaddressblock').show('slow');
}
else {
$('#deliveryaddressblock').hide(1000);
}
});
});
</script>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2" class="colourrow"><h4>Contact Address</h4></td>
</tr>
<tr>
<td><?php echo $form->labelEx($model,'company'); ?></td>
<td colspan="2" >
<?php echo $form->textField($model,'company',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'company'); ?>
</td>
</tr>
<tr>
<td colspan="2">Contact Person</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model,'firstname'); ?></td>
<td><?php echo $form->textField($model,'firstname',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'firstname'); ?>
</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model,'lastname'); ?></td>
<td><?php echo $form->textField($model,'lastname',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'lastname'); ?>
</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model,'street_address'); ?></td>
<td>
<?php echo $form->textField($model,'street_address',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'street_address'); ?>
</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model,'suburb'); ?></td>
<td>
<?php echo $form->textField($model,'suburb',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'suburb'); ?>
</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model,'postcode'); ?></td>
<td>
<?php echo $form->textField($model,'postcode',array('size'=>20,'maxlength'=>60)); ?>
<?php echo $form->error($model,'postcode'); ?>
</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model,'city'); ?></td>
<td>
<?php echo $form->textField($model,'city',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'city'); ?>
</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model,'state'); ?></td>
<td>
<?php echo $form->textField($model,'state',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'state'); ?>
</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model,'country_id'); ?></td>
<td>
<?php echo CHtml::dropDownList('TsAddressBook[country_id]', $model->country_id, CHtml::listData($countries,'countries_id','countries_name'), array('empty' => '--Please Select--')); ?>
<?php echo $form->error($model,'country_id'); ?>
</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model,'telephone'); ?></td>
<td>
<?php echo $form->textField($model,'telephone',array('size'=>20,'maxlength'=>60)); ?>
<?php echo $form->error($model,'telephone'); ?>
</td>
</tr>
<tr>
<td><?php echo $form->labelEx($model,'fax'); ?></td>
<td>
<?php echo $form->textField($model,'fax',array('size'=>20,'maxlength'=>60)); ?>
<?php echo $form->error($model,'fax'); ?>
</td>
</tr>
<tr>
<td colspan="2">
<?php echo MyHtml::checkBox('_checkBilling', false, array('id'=>'_checkBilling'));?>
<?php echo MyHtml::label('Show/Hide Billing Address', 'showhidebillingblock') ?>
</td>
</tr>
<tr>
<td colspan="2">
<?php echo MyHtml::checkBox('_checkDelivery', false, array('id'=>'_checkDelivery'));?>
<?php// echo MyHtml::checkBox($model, '_checkDelivery');?>
<?php echo MyHtml::label('Show/Hide Delivery Address', 'showhidedeliveryblock') ?>
</td>
</tr>
</table>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
<?php echo MyHtml::button('Cancel', array('id'=>'butCancel')); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Thanks for your help. I am looking forward your reply.