Z
Z
ZaurK2016-11-02 10:39:02
Yii
ZaurK, 2016-11-02 10:39:02

How to correctly pass the selection of a dropdownlist multiselect to write to the database in yii2?

Hello! I have a form with a droplist multiselect

<?php $form = ActiveForm::begin(); ?>
    <?= $form->field($model, 'dtitle')->textInput(['maxlength' => true]) ?>  
    <?= $form->field($model, 'parts')->dropDownList($model->IngredientDropdown,
                                                      [
                                                          'multiple' => 'multiple'
                                                      ]
                                                     ); ?>
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Добавить' : 'Редактировать', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>
    <?php ActiveForm::end(); ?>

In the model:
public function getIngredientDropdown()
    {
        $listIngredient = Ingredient::find()->select('id,ititle')->all();
        $list = ArrayHelper::map( $listIngredient, 'id', 'ititle');
        return $list;      
    }

and controller
public function actionCreate()
    {
        $model = new Dish();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $model->parts = implode(",", $_POST['parts']);
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

I want to transfer data for writing to the database, and the selected selects as a string separated by commas. But it gives the error "The value of 'Parts' is invalid". Please advise what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2016-11-02
@qonand

Those. In fact, you want that when submitting the form, the data would be saved to the associated table along with saving the model? If so, then you are reinventing the wheel. Read this article on habré

M
Maxim Timofeev, 2016-11-07
@webinar

1. if the choice is multiple, then change
to
2. here:

if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $model->parts = implode(",", $_POST['parts']);

You saved at the beginning, then do implode - what's the point? In addition, $_POST['parts'] probably does not exist, most likely $_POST['model name']['parts']
3. Instead of implode, it's better to create another table, link with this one and store the values ​​there, but in any case I would not use implode, it is better to use yii\helpers\Json and put this functionality into the beforeSave
4 model.
But it gives the error "The value of 'Parts' is invalid".
There is no such error, give an example of a real error. Although it will go away if you fix the above. By the way, in the rules of the model, make it safe for parts, since this is an array and will not be validated.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question