A
A
AlexAll2018-11-06 15:13:08
Yii
AlexAll, 2018-11-06 15:13:08

How to load form fields with ajax depending on category selection in Yii2?

When creating a post
I have installed the Select2 widget from Kartik demos.krajee.com/widget-details/select2
And I use it as Ajax Loading - you enter 3 letters and it finds the right category. It is necessary to make it so that after the category is found and selected, an additional field is displayed with a radio list of site names with links.

<?= $form->field($model, 'site')->radioList(ArrayHelper::map(Site::getSite($category_id), 'link', 'title'),  
            [
                                
        'item' => function($index, $label, $name, $checked, $value) use ($model) {
            $check = $checked == 1 ? "checked='checked'" : null;
            return '<label class="modal-radio" style="display:block;">
                  <input type="radio" name="' . $name . '" value="' . $value . '" ' . $check . ' tabindex="3">
                  <i></i>
                  <span>' . ucwords($label) . '</span>
               </label>';
    }
                            ]
                        ); ?>

I insert this field into a separate post/form_specifics file.
I try to do it using a jquery script, when you find a field in Ajax Loading and select a category, an ajax request to /post/post-specifics is triggered and the Post is sent with a category_id request and if everything is ok, then it displays additional code coming from /post/post-specifics
jQuery(function ($) {
$(document).on("select2:select", "#post-category_id", function(e) {
    if (($("#post-category_id").val() != null)) { 
                $.ajax({
                    url: "/post/post-specifics",
                    data: {
                        category_id: $("#post-category_id").val(),
                    },
                    method: "POST",
                    beforeSend: function(xhr) {
                        $(".wrapper_post_specifics").html("<h2 class=\"text-center\">Загрузка...</h2>");
                    }
                }).done(function(data) {
                    $(".wrapper_post_specifics").html(data);
                }).fail(function(data) {
                    alert("Во время загрузки Дополнительных полей потока произошла ошибка!");
                });
            }
   
})};

in _form.php I put it in the right place inside <?php $form = ActiveForm::begin(); ?>
How to properly render post/form_specifics in action ? with the transfer of all data?
I try this way
in the Post controller I do an action
public function actionPostSpecifics(){
          $model = new Post();
          $model->load(Yii::$app->request->post());
          $ category_id = $model-> category_id;

    
          return $this->render('form_specifics', [
           'model' => $model ,
            'category_id' => $category_id,
        ]);
    }

But this is not correct, it starts to display errors that there is no form variable, I put <?php $form = ActiveForm::begin(); ?> and end then ArrayHelper, etc. when I write all use, the script works but displays post / form_specifics instead of one field the whole page with the main layuot
how to make actionPostSpecifics so that this field is rendered within the actionCreate and its <?php $form = ActiveForm: :begin(); ?> and then everything is passed to actionCreate as a single post request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-11-06
@AlexAll

Good evening.

public function actionPostSpecifics(){
    if(Yii::$app->request->isAjax){
        $model = new Post();
        $model->load(Yii::$app->request->post());
        $category_id = $model-> category_id;   
        
        $form = new \yii\widgets\ActiveForm;
        
      return $this->renderAjax('form_specifics', [
          'model' => $model ,
          'category_id' => $category_id,
          
          'form' => $form
          
      ]);       
    }
}

In the form_specifics file, use $form in the usual way
echo $form->field($model, 'attribute_name')->textInput();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question