Multiple select2

  • Pippo Poppo
    Participant
      # 2 months, 3 weeks ago

      Hi,

      Hi, the multiple selection of the values of some select-boxes is possible?.
      Here an example. How can I make it a multiple choice of its values?
      Thank you

      ‘tax_and_currency’ => [

      ‘tax_id’ => [
      ‘type’ => ‘manyToOne’,
      ‘relation’ => ‘tax’,
      ‘column’ => ‘name’,
      ‘col-class’ => ‘col-lg-4 col-md-4 col-sm-6’,
      ‘dont_translate’ => true,
      ],

    • laravel-bap
      Keymaster
        # 2 months, 3 weeks ago

        Hi,

        just add ‘multiple’ => true

        $this->add(‘roles’, ‘choice’, [
        ‘choices’ => $roles,
        ‘attr’ => [‘class’ => ‘select2 pmd-select2 form-control’],
        ‘selected’ => $this->model ? $this->model->roles()->pluck(‘name’)->toArray() : null ,
        ‘expanded’ => false,
        ‘multiple’ => true
        ]);


        remember that you will need to implement relation one to many or many to many.

      • Asif Ba
        Participant
          # 2 months, 3 weeks ago

          This is working with normal forms. However, in ‘modals’ I am unable to do keyboard inputs to select from the list.

        • laravel-bap
          Keymaster
            # 2 months, 2 weeks ago

            I checked this on new version (not yet published) and there is no issue. I don’t think i changed anything in this place. Below my setting. I use CTRL to select multiple records from dropdown.

          • Pippo Poppo
            Participant
              # 3 weeks, 5 days ago

              Hi again,

              I managed to replace my text input with a multiple choice and it’s working:

              Now I have this issue:
              Illuminate \ Database \ QueryException
              Array to string conversion (SQL: update secdocuments set updated_at = 2019-04-30 13:54:50, down_by = user-2, notify_by = user-2, closing_date = 2019-04-25 00:00:00 where id = 2)

              The fields I’m trying to update are: down_by and notify_by .
              But it isn’t saving the entire array into the database field. I was expecting that it would work as the database field is a varchar.

              Thanks for your help.

            • laravel-bap
              Keymaster
                # 3 weeks, 5 days ago

                Hi,

                I think before saving array to database you need to serialize or convert to json and then save.

                https://www.php.net/manual/en/function.serialize.php
                https://www.php.net/manual/en/function.json-encode.php

              • Pippo Poppo
                Participant
                  # 3 weeks, 5 days ago

                  Ok, I’m going crazy.. 😀

                  Serializing the array is indeed correct, actually I don’t know where to put the function to convert the array.
                  But:
                  What I basically need is an input in my form, to select multiple choices and save those choices – the view has to be capable of interpreting the saved data and convert ‘user-2’ to ‘John Smith’.

                  So far I managed to select multiple choices taking the data from users and groups table:
                  ‘choices’ => FormHelper::downByChoises()
                  ___
                  public static function downByChoises()
                  {

                  if(!empty(Landlord::getTenants()->first())){
                  $users = User::where(‘company_id’,’=’,Landlord::getTenants()->first())->get();
                  $groups = Group::where(‘company_id’,’=’,Landlord::getTenants()->first())->get();
                  }else{
                  $users = User::all();
                  $groups = Group::all();
                  }

                  $options = [
                  trans(‘core::core.form.optgroup.users’) => $users->mapWithKeys(function ($item) {
                  return [‘user-‘ . $item[‘id’] => $item[‘name’]];
                  })->toArray(),
                  trans(‘core::core.form.optgroup.groups’) => $groups->mapWithKeys(function ($item) {
                  return [‘group-‘ . $item[‘id’] => $item[‘name’]];
                  })->toArray(),
                  ];

                  return $options;
                  }

                  I’m not sure in the controller how to configure this:
                  protected $showFields = [
                  ‘down_by’ => [‘type’ => ‘manyToMany’, ‘column’ => ‘name’, ‘relation’ => [‘users’,’groups’], ‘col-class’ => ‘col-lg-4’],
                  ‘notify_by’ => [‘type’ => ‘manyToMany’, ‘column’ => ‘name’, ‘relation’ => [‘users’,’groups’], ‘col-class’ => ‘col-lg-4’]
                  ]

                  I’m not sure what type to use.
                  oneToMany, manyToMany, manyToOne are giving me errors related to relations.

                  The thing is: these two fields have to save users name and group name.
                  Do I need a specific relation? Or it is sufficient a text type? With a json saved into ?

                  Thanks,

                  have a nice day.

                • laravel-bap
                  Keymaster
                    # 3 weeks, 3 days ago

                    If you have this in git send me access to support@laravel-bap.com i will try to help.

                  • Pippo Poppo
                    Participant
                      # 3 weeks, 1 day ago

                      Unfortunately there is a secrecy obligation on this project, I’m not free to expose it but I will ask for it. Thank you very much.

                      In the mean time I’ll ask you this:
                      in BAP where exactly is the source code that do the savings when I submit a form?
                      In a simple, common module like contacts: I create a new contact and a form appear to me.
                      What I need to know right now, is the flow. Who call what and when when I open a form to save a new contact; who call what and when when I open the page that make me see the list of the contact I saved earlier.

                      Thanks for your help.

                    • laravel-bap
                      Keymaster
                        # 3 weeks ago

                        Hi,

                        Crud Controller

                        All crud module extends “ModuleCrudController” in this controller You have.

                        index – This is list of records

                        create – This is show create form.

                        show – This is show view.

                        store – This is store/post new record.

                        edit – This is show edit view.

                        update – post update .

                        destroy – delete record.

                        Is that what You wanted to know?

                      • Pippo Poppo
                        Participant
                          # 1 week, 4 days ago

                          Hi,

                          yes that’s exactly that. Thanks a lot 🙂

                          Daniele.

                        You must be logged in to reply to this topic.