Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select multiple changes name from users to users[][] #150

Closed
zalazdi opened this issue Aug 25, 2015 · 8 comments · Fixed by #151
Closed

Select multiple changes name from users to users[][] #150

zalazdi opened this issue Aug 25, 2015 · 8 comments · Fixed by #151

Comments

@zalazdi
Copy link

zalazdi commented Aug 25, 2015

After upgrade to 1.6.20, field Select with multiple attr changes field name from users to users[][](double brackets).
I noticed that FormField@prepareOptions are executed two times.

@kristijanhusak
Copy link
Owner

Is it changing from users to users[] or to users[][] ?

@zalazdi
Copy link
Author

zalazdi commented Aug 25, 2015

From users to users[][]

@kristijanhusak
Copy link
Owner

Can you please give me your form structure so i can test? Thanks.

@zalazdi
Copy link
Author

zalazdi commented Aug 25, 2015

It's little messy, but it worked in previous version (1.6.12).

<?php namespace App\Forms;

use Carbon\Carbon;
use Kris\LaravelFormBuilder\Form;

class CalendarForm extends Form
{
    public function buildForm()
    {
        $this->setOptions();

        $this->add('title', 'text', [
            'label' => 'Tytuł wydarzenia',
        ]);

        $this->add('note', 'textarea', [
            'label' => 'Opis',
            'attr' => [
                'rows' => 3
            ]
        ]);

        $this->add('all_day', 'checkbox', [
            'label' => 'Cały dzień',

            'wrapper' => [
                'class' => 'form-group'
            ],
        ]);

        $this->add('start', 'datetime', [
            'label' => 'Rozpoczęcie',
        ]);

        $this->add('duration', 'number', [
            'label' => 'Czas trwania w minutach',
        ]);

        $this->add('users', 'select', [
            'label' => 'Kto będzie na spotkaniu',
            'choices' => $this->getUsers(),
            'attr' => [
                'class' => 'form-control select2',
                'multiple' => 'multiple'
            ]
        ]);

        $this->add('color', 'color', [
            'label' => 'Kolor etykiety',
        ]);

        $this->add('submit', 'submit', [
            'label' => property_exists($this->getModel(), 'id') ? 'Edytuj wydarzenie' : 'Dodaj wydarzenie',
            'attr' => [
                'name' => 'calendar',
                'value' => 1
            ]
        ]);
    }

    private function setOptions()
    {
        $this->setFormOption('method', 'POST');

        if ($this->getFormOption('application')) {
            $this->setFormOption('url', route('application.flow'));
        } else {
            if ($this->getModel()) {
                $this->setFormOption('url', route('task.update'));
            } else {
                $this->setFormOption('url', route('task.store'));
            }
        }

        if ($this->getModel()) {
            $this->setModelProperties();
        }
    }

    private function getUsers()
    {
        $selected = [\Sentinel::getUser()->id];

        $users = [
            \Sentinel::getUser()->id => 'Ja',
        ];

        $app = $this->getFormOption('application');
        if($app) {
            $users[$app->candidate->id] = $app->candidate->full_name;
            $selected[] = $app->candidate->id;

            foreach(\App\Entities\User::whereWorker(1)->where('id', '!=', \Sentinel::getUser()->id)->get() as $worker) {
                $users[$worker->id] = $worker->full_name;
            }
        } else {
            foreach (\App\Entities\User::all() as $worker) {
                $users[$worker->id] = $worker->full_name;
            }
        }

        $this->setSelectedUsers($selected);

        return $users;
    }

    private function setSelectedUsers($ids = [])
    {
        if (!$this->getModel()) {
            $model = new \stdClass();
            $model->users = $ids;

            $this->setModel($model);
        }
    }

    private function setModelProperties()
    {
        $model = $this->getModel();

        $this->add('task_id', 'hidden', [
            'value' => $model->id
        ]);

        $model->users = $model->workers->lists('id')->toArray();
    }
}

@kristijanhusak
Copy link
Owner

@zalazdi
Can you try pulling in dev-master and test if everything works for you?

@zalazdi
Copy link
Author

zalazdi commented Aug 26, 2015

Yeah, now it's work. But i got another error. I'm opening new issue.

@hackel
Copy link
Contributor

hackel commented Sep 21, 2015

I'm getting hit by this as well. Any chance you can release a new version including the fix in #151?

@kristijanhusak
Copy link
Owner

@hackel @zalazdi I released new version (1.6.30) which includes this fix. Thanks for reporting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants