0

here is code of dropdownlist.. but when I select multiple values it gives validation error "task must be string"

how to save multiple values (array)?

   <?php echo $form->field($model, 'task')->widget(Select2::classname(), [
                            'data' => $companiesList,
                            'options' => ['placeholder' => 'Select company...','multiple' => true],
                            'pluginOptions' => ['allowClear' => true,],
                        ]);?>

how to give checkbox for each value in list?

3
  • in model rules set [['task'], 'safe'] from [['task'], 'string']. Commented Dec 27, 2017 at 13:01
  • PHP Warning – yii\base\ErrorException quoted_printable_encode() expects parameter 1 to be string, array given Commented Dec 27, 2017 at 13:12
  • quoted_printable_encode comes from your wrong rule arr paste all rule arr here to help more Commented Oct 26, 2020 at 9:33

2 Answers 2

0

you have to save multiple values in many-to-many tabels ,

after changing the rules to [['task'], 'safe'] from Mr Skull answer , you have to get all data like this :

foreach ( $model->task as $single_task){

$task = new _many_to_many_model();
$task->side_1_id = single_task;
$task->side_2_id = $model->id;
$task->save();

}

after this comment :

all selected value should go in single column

you don't need to use many-to-many ! I use "-" as delimiter,

$all_taskes = "";
    foreach ( $model->task as $single_task){

    $all_taskes .= single_task."-";

    }

$model->task = all_taskes;
Sign up to request clarification or add additional context in comments.

9 Comments

in controller after $model->save() , but it's important to where do you want to save multiple data ? and let me know in single column or many-to-many ?
all selected value should go in single column
Okey , let see my next edit to answer and put in in your controller .. forget the many to many ...
check it out , and see part 2
error...... PHP Warning – yii\base\ErrorException quoted_printable_encode() expects parameter 1 to be string, array given
|
0

Hi there ,

       $model->save();
       foreach ($model->task as $cat) {
    
                    $m = new \common\models\_many_to_many_model();
                    $m->ads_id = $model->id; // change in to yorr base model ID and use it's put after $model->save
                    $m->category_id = $cat; // category_id is your many to many model filed id 
                    $m->save();
       }

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.