Form is not getting submitted when render partial in Yii

1.8k views Asked by At

I am partially rendering a form in a view in which user can add subaccounts(profiles). The view in which the form is called is another form where all subaccounts are listed as radiolist.

Below is my view.

<div class="inputs">
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'selectUser-form',

                        'enableAjaxValidation' => false,
)); ?>

    <?php echo CHtml::activeRadioButtonList($model,'id', $model->getSubAccounts(),array('prompt'=>'Please Select'));?>

        <?php echo CHtml::submitButton($model->isNewRecord ? 'Book Now' : 'Save'); ?>
        <?php echo CHtml::Button('Cancel',array('submit'=>array('cancel','id'=>$model->a_id)));?>


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



<div id="data"></div>
<?php echo CHtml::ajaxButton ("Add Users",
                              CController::createUrl('/user/user/addSubAccount'),
                              array('update' => '#data'));
?>

Below is my addSubAccount action.

public function actionAddSubAccount()
    {
    $profile=new YumProfile;

         if (isset($_POST['YumProfile']))
        {
                    $profile->attributes = $_POST['YumProfile'];
                $profile->user_id=Yii::app()->user->id;

                if($profile->save())
                $this->redirect(array('/home/create'));}

if(!Yii::app()->request->isAjaxRequest){
    $this->render('subaccount_form', array('profile'=>$profile));}
    else{
    $this->renderPartial('subaccount_form', array('profile'=>$profile));

    }
}

Below is subaccount_form.

<?php $this->title = Yum::t('SubAccounts');?>
<div class="wide form">
<?php $activeform = $this->beginWidget('CActiveForm', array(
            'id'=>'subaccount-form',
            'enableAjaxValidation'=>true,
            'enableClientValidation'=>true,
            'clientOptions' => array(
                            'validateOnChange' => true,
                            'validateOnSubmit' => true,
                        ),

            ));
?>

<div class="row"> <?php
echo $activeform->labelEx($profile,'Firstname');
echo $activeform->textField($profile,'firstname');
echo $activeform->error($profile,'firstname');
?> </div>

<div class="row"> <?php
echo $activeform->labelEx($profile,'Lastname');
echo $activeform->textField($profile,'lastname');
echo $activeform->error($profile,'lastname');
?> </div>

<div class="row"> <?php
echo $activeform->labelEx($profile,'Age');

echo $activeform->textField($profile,'age');
echo $activeform->error($profile,'age');
?> </div>

<div class="row submit">
        <?php echo CHtml::submitButton(Yum::t('Add')); ?>
</div>
<?php $this->endWidget(); ?>

My form is rendering. But it's not getting submitted.What am i doing wrong?

After submitting,I need the view to be refreshed and to display the newly added user as an option in the radio list.

EDIT:

I tried like adding the following in the CActiveForm Widget array:

'action' => array( '/user/user/addsubAccount' ),

But still no result.Instead it is saving my data two times when i go through my direct way,meaning render method. :-(

2

There are 2 answers

0
Let me see On

It is because

'enableAjaxValidation'=>true,

Ajax validation is set to true in your form. Set its value to false

'enableAjaxValidation'=>FALSE,

and then your form will submit :) and if you want it to be true only then you should uncomment

$this->performAjaxValidation($model);

in your controller's action

Update 1

    if(!Yii::app()->request->isAjaxRequest){
        $this->render('subaccount_form', array('profile'=>$profile));}
        else{
//change this line 
        $this->renderPartial('subaccount_form', array('profile'=>$profile),FALSE,TRUE);

        }

This link might help you

0
dsl400 On

on renderPartial the action attribute of the form is set to the current page instead of being set to the actual update or create url

My Solution

$form=$this->beginWidget('CActiveForm', array(
    'id'=>'country-form',
    'action' => Yii::app()->createUrl(is_null(Yii::app()->request->getParam('id'))?'/country/create':'/country/update/'.Yii::app()->request->getParam('id')),