Hello comrades,

The problem is this: I try using the kartik select2 drop-down list to make a multiple selection of letter recipients in this way:

<?= $form->field($model, 'receiver_id')->widget(Select2::classname(), [ 'data' => ArrayHelper::map(User::find()->all(),'id','username'), 'options' => ['placeholder' => 'Select receivers...', 'multiple' => true], 'pluginOptions' => [ 'tags' => true, 'maximumInputLength' => 10 ], ]); ?> 

But when you click the "Send" button under the Receiver ID text box, the error "Receiver ID must be an integer" is displayed in red.

Please explain how to use this list correctly or provide alternative solutions.

Thank you in advance.

  • Are you sure that in the case of a multiple choice, the field type in the database should be an int ? Maybe all the same string ? - MasterAlex
  • I need the same email to be sent to several users, i.e. Record was duplicated for different receiver_id. Unless if I change field type on string it will solve my problem? - Vitaly Park
  • Yes, if you turn out the resulting array when sending letters - MasterAlex
  • Can you give an example of such a search? - Vitaly Park
  • The usual loop, try so foreach($model->receiver_id as $user_id) { echo $user_id.'<br />'; } foreach($model->receiver_id as $user_id) { echo $user_id.'<br />'; } . If this cycle displays the id of users who need to send a letter, then you just need to fasten the logic of sending the letter and add the rest of the repeating data from the model in this cycle. - MasterAlex

1 answer 1

Like a correct mistake. At you hasOne and it is not hasMany

 <?= $form->field($model, 'receiverIDs')->widget(Select2::classname(), [ 'data' => ArrayHelper::map(User::find()->all(),'id','username'), 'options' => [ 'placeholder' => 'Select receivers...', 'multiple' => true, ], 'pluginOptions' => [ 'tags' => true, 'maximumInputLength' => 10, ], ]); ?> 

And in the model

 class Model extends base\ActiveRecord { public function rules() { return [ //.... ['receiverIDs', 'each', 'rule' => ['integer']] ]; } public $receiverIDs = []; } 

Nepomnyu already as Select2 with these settings saves. Either separated by a comma as a string, then you need explode(',' , $this->receiverIDs) or as an array of aids. See what is being formed in the request.