在Yii中渲染部分时,表单未提交

时间:2013-12-31 09:49:45

标签: ajax yii yii-cactiverecord

我在视图中部分渲染表单,用户可以在其中添加子帐户(配置文件)。调用表单的视图是另一种形式,其中所有子帐户都列为radiolist。

以下是我的观点。

<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'));
?>

以下是我的addSubAccount操作。

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));

    }
}

下面是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(); ?>

我的表单正在渲染。但它没有被提交。我做错了什么?

提交后,我需要刷新视图并将新添加的用户显示为广播列表中的选项。

编辑:

我尝试在CActiveForm Widget数组中添加以下内容:

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

但仍然没有结果。当我直接通过render方法时,它仍然保存了我的数据两次。 : - (

2 个答案:

答案 0 :(得分:0)

这是因为

'enableAjaxValidation'=>true,

在表单中将Ajax验证设置为 true 。将其值设置为 false

'enableAjaxValidation'=>FALSE,

然后你的表格会提交:) 如果你想要它是真的那么你应该取消注释

$this->performAjaxValidation($model);

在你的控制器的行动中

更新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);

        }

link可能对您有所帮助

答案 1 :(得分:0)

在renderPartial上,表单的action属性设置为当前页面,而不是设置为实际更新或创建URL

我的解决方案

$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')),
相关问题