A
A
Alex2021-08-24 18:46:56
Yii
Alex, 2021-08-24 18:46:56

How to output data in yii2-multiple-input widget after save?

For half a day I have been suffering with the output of data through the yii2-multiple-input widget does not want to work.
There are few examples in the documentation for me personally.
The data is stored in my database in 5 cells.
Before adding any entry via MultipleInput, there are no errors and the form on the page works as it should. As soon as I add at least 1 record (or 5), the form works out, everything is saved to the database and the page with the error stops opening:

Error
Call to a member function hasErrors() on array
1. in W:\domains\finderlot.ru\yii2\vendor\yiisoft\yii2\widgets\ActiveField.php


I understand that the form is waiting for 1 entry for output, but an array comes? But on the other hand, if I substitute a link (the same data but only through a link) <?= $form->field($model->priceschange, 'prices_list')->widget(MultipleInput::className() , then the page again starts to

open.In the field widget form, I call it like this:
<?= $form->field($model_price_change, 'prices_list')->widget(MultipleInput::className(), [
                                        'id' => 'price_change',
                                        'max'               => 50,
                                        'min'               => 0,
                                        'allowEmptyList'    => false,
                                        'enableGuessTitle'  => true,
                                        'sortable' => true,
                                        //'data' => $model_price_change,
                                        'columns' => [
                                            [
                                                'name'  => 'date_start',
                                                'title' => 'Дата начала',
                                                'type' => DateTimePicker::className(),
                                                'options' => [
                                                    'pluginOptions' => [
                                                        'minView' => '3',
                                                        'maxView' => '3',
                                                        'format' => 'dd.mm.yyyy hh:ii:ss', // формат который будет передаваться в базу
                                                        'autoclose' => true,
                                                        'weekStart' => 1,
                                                        'startDate' => date('01.01.2020'), //дата ниже которой нельзя установить значение
                                                    ]
                                                ]
                                            ],
                                            [
                                                'name'  => 'date_end',
                                                'title' => 'Дата окончания',
                                                'type' => DateTimePicker::className(),
                                                // 'value' => function($data) {
                                                //     return $data['date_end'];
                                                // },
                                                'options' => [
                                                    'pluginOptions' => [
                                                        'minView' => '3',
                                                        'maxView' => '3',
                                                        'format' => 'dd.mm.yyyy hh:ii:ss', // формат который будет передаваться в базу
                                                        'autoclose' => true,
                                                        'weekStart' => 1,
                                                        'startDate' => date('01.01.2020'), //дата ниже которой нельзя установить значение
                                                    ]
                                                ]
                                            ],
                                            [
                                                'name'  => 'price',
                                                'title' => 'Цена',
                                                'type' => 'textInput',
                                                // 'value' => function($data) {
                                                //     return $data['price'];
                                                // },
                                                'options' => [
                                                    'placeholder' => 'В рублях',
                                                ]
                                            ],
                                            [
                                                'name'  => 'deposit',
                                                'title' => 'Задаток',
                                                'type' => 'textInput',
                                                // 'value' => function($data) {
                                                //     return $data['deposit'];
                                                // },
                                                'options' => [
                                                    'placeholder' => 'В рублях',
                                                ]
                                            ],
                                        ],
                                        'iconSource' => 'fa',
                                        'addButtonOptions' => [
                                            'class' => 'btn btn-success',
                                        ],
                                        'addButtonPosition' => MultipleInput::POS_HEADER
                                    ])
                                    ->label(false);
                                ?>


In the controller, I load the data like this:
$model_price_change = oldPrices::find()->where(['lot_id' => $id])->all();

if($model_price_change == null && empty($model_price_change))
{
    return new oldPrices();
} else {
    return $model_price_change;
}


In the model like this:
public $prices_list;

public function rules()
    {
        return [
            [['price', 'deposit'], 'string'],
            [['created_at', 'updated_at', 'date_start', 'date_end', 'prices_list'], 'safe'],
        ];
    }

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
V
vilinyh, 2021-08-24
@braun_alex

$model_price_change must contain one model:

<?= $form->field($model_price_change, 'prices_list')

And you have an array there:
$model_price_change = oldPrices::find()->where(['lot_id' => $id])->all();

And then there are two options that depend on connections and models. Or you are looking for a model with the wrong method and you need to use findOne():
$model_price_change = oldPrices::findOne(['lot_id' => $id]);

Or you're using the wrong model on the form altogether, and what you really need to do is to separate the post model in the widget and the form model:
$model_price_change = new FormModel();
$model_price_change->prices_list= oldPrices::find()->where(['lot_id' => $id])->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question