V
V
Vitaly Khvan2018-03-12 10:12:29
Yii
Vitaly Khvan, 2018-03-12 10:12:29

Yii2 PHP How to fix url generation due to filters in searchController?

In general, there is a view in which there is a listView and an activeForm, everything is wrapped in pjax, when trying to enter a new filter (for example, "From 1000 to 10000" route in the url is built as /site/catalog?start_retail[]=1000&end_retail[]=10000 Now everything is correct, but when I want to enter a different range of numbers, it turns out that the get parameters are not replaced, but generated by /site/catalog?start_retail[]=1000&end_retail[]=10000&start_retail[]=99999&end_retail[]=999999
CatalogController

public function actionGet()
    {
        $model = $this->modelFindBySlug($this->slug);

        $searchModel = new ProductCatalogSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams,$model->id);

        $brands = $model->brands;
        $weights = $model->weights;


        $test['start'] = Yii::$app->request->get('ProductCatalogSearch')['start_retail_price'];
        $test['end'] = Yii::$app->request->get('ProductCatalogSearch')['end_retail_price'];

        if($test['start']){
            $defaultPrice['min'] = $test['start'];
        }else{
            $defaultPrice['min'] = $model->minPrice;
        }

        if($test['end']){
            $defaultPrice['max'] = $test['end'];
        }else{
            $defaultPrice['max'] = $model->maxPrice;
        }

        $defaultPrice['max'] = $model->maxPrice;

        return $this->render('get',[
            'model' => $model,
            'dataProvider' => $dataProvider,
            'search' => $searchModel,
            'brands' => $brands,
            'weights' => $weights,
            'defaultPrice' => $defaultPrice
        ]);
    }

View Get.php
<?php

use yii\widgets\ListView;
use yii\widgets\Pjax;

$this->title = 'Каталог';
Yii::$app->view->params['page'] = 'list';

?>
<?php Pjax::begin(['id' => 'catalogPjax','options' => ['class'=> 'pjaxContainer','timeout' => 2000, 'enableReplaceState' => true, 'clientOptions' => ['method' => 'GET']]])?>
    <div class="col-lg-3">
        <div class="filter">
            <?= Yii::$app->controller->renderPartial('filters/_filter',['model' => $search,'brands' => $brands, 'weights' => $weights,'defaultPrice' => $defaultPrice]) ?>
        </div>
    </div>
    <div class="col-lg-9">
        <div class="filter-panel clearfix">
            <h1><?= $model->name ?></h1>
        </div>
        <ul class="list-items row">
                <?php echo ListView::widget([
                    'dataProvider' => $dataProvider,
                    'itemView' => '_item',
                ]); ?>
        </ul>
    </div>
<?php Pjax::end();?>

Particle_filter.php
<?php

use common\helpers\Formatter;
use yii\bootstrap\Html;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;

$autolick = <<<JS
    $('.filter_input').change(function() {
        $('#filterButton').click();
    });
JS;

$this->registerJs($autolick);

?>
<?php $form = ActiveForm::begin(['method' => 'GET', 'options' => ['data-pjax' => true]]) ?>

<button data-pjax=0 id="filterButton" class="hidden"></button>

<h3>Производитель</h3>

<ul>
    <?= $form->field($model, 'brand_id')->checkboxList(ArrayHelper::map($brands, 'id', 'name'), [
        'itemOptions' => ['class' => 'filter_input'],
    ])->label(false) ?>
</ul>

<h3>Цена <span> (тг) </span></h3>

<hr>
<span>От:</span>
<?= $form->field($model, 'start_retail_price')->textInput(['class' => 'filter_input', 'value' => $defaultPrice['min']])->label(false) ?>
<span>По:</span>
<?= $form->field($model, 'end_retail_price')->textInput(['class' => 'filter_input', 'value' => $defaultPrice['max']])->label(false) ?>
<hr>

<?php if ($weights) : ?>

    <h3>Вес упаковки</h3>
    <ul>
        <?= $form->field($model, 'weight_net')->checkboxList(ArrayHelper::map($weights, 'weight_net', 'weight_net'), [
            'item' => function ($index, $label, $name, $checked, $value) {
                return Html::checkbox($name, $checked, ['label' => Formatter::asWeight($label), 'id' => $index, 'value' => $value, 'class' => 'filter_input']);
            }
        ])->label(false) ?>
    </ul>

<?php endif; ?>

<?php ActiveForm::end(); ?>

Now the question is how to indicate in ActiveForm that an attribute cannot be an array, and was replaced when the data in it changed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2018-03-12
@webinar

Somewhere a leading slash is missing, probably in the form action.
yii has nothing to do with it, like php

C
cheshirko, 2019-09-17
@cheshirko

I understand that the question is old, but I’ll write it off, suddenly it will come in handy for someone:
the problem is solved by adding the name attribute to the form active fields, then it stops displaying with an array

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question