0

I'm having an issue with submitting a TbActiveForm and the renderPartial wiping my page out and displaying only the partial view. I want to reload only the widget after my action triggers and finishes. I'm also using a modal to display and make changes.

view:

$form = $this->beginWidget(
     'booster.widgets.TbActiveForm',
     array(
         'id' => 'horizontalForm',
         'type' => 'horizontal',
         'action' => Yii::app()->createUrl('orderControl/order/returns.save'),
          )
     );
     echo $form->hiddenField(
          $editReturnFormModel,
          'orderId',
          array(
              'value' => $editReturnFormModel->orderId
         )
     );
     $this->widget(
           'bootstrap.widgets.TbButton',
           array('buttonType' => 'submit', 'type' => 'primary', 'label' => 'Save')
                        );
     $this->endWidget();

Action:

$this->controller->renderPartial('ScModules.orderControl.widgets.ReturnsWidget.views._returnItems', array('returnsDataProvider'=>$returnsDataProvider, 'editReturnFormModel'=>$editReturnFormModel));

One other point is that the Yii::app()->createUrl('orderControl/order/returns.save') is change the page url all together. On this page page I'm directed to, the view is created fine. I need the widget to rebuild/refresh on the current page and not send me somewhere else. Any ideas on solution would be appreciated.

1 Answer 1

1

Here's what I would do:

  1. Wrap your form widget inside a div or whatever block tag you like.<div id="myFormWrapper"> (your widget goes here) </div>
  2. Add a custom ID in your form (id="formId") and submit button (id="submitButtonId")
  3. Add some jQuery in order to submit your form and replace the old widget with the new


    $(document).on('click', '#submitButtonId' , function() {
        $.ajax({
            type: 'POST',
            url: $('#formId').attr('action'),
            data : $('#formId').serialize(),
            beforeSend : function(){
                    //do anything you want before sending the form
            },
            success : function(data){
                    //We'll replace the old form with the new form widget  
                    $('#myFormWrapper').html(data);

            },
            error : function(data){
                console.log('ops');
            },
        });
        return false;
    });

  1. Do whatever you want to do in your Controller action and use renderPartial.


    //The action below should be the same that you used in the action attribute of the form
    public function actionProcessForm(){
       $model = new MyFormModelName();
       if(isset($_POST['MyFormModelName'])){
          $model->attributes = $_POST['MyFormModelName'];
          if($model->save()){
             //Do whatever you want here
             $returnsDataProvider = new CActiveDataProvider('YourModel');
             $this->renderPartial('//folder/to/your/view',  array('returnsDataProvider'=> $returnsDataProvider, 'editReturnFormModel'=>$editReturnFormModel));
          }else{
              //You might want to render something else here
              $this->renderPartial('errorViewPage');
          }
       }
    }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.