metalowy
metalowy

Reputation: 111

Laravel - Form select multiple

what I am trying to do is to convert standard Blade select form with multiple options where you must select options with ctrl pressed to one where you use checkboxes to select multiple choices.

My form look like this:

{!! Form::open(array('action' => 'UserController@visit_step_two', 'method' => 'post')); !!}
    {!! Form::select('services', $servicesList, null, array('multiple'=>'multiple','name'=>'services[]')); !!}
    {!! Form::submit('Next'); !!}
    {!! Form::close(); !!}

What should I do to change that?

Upvotes: 2

Views: 3253

Answers (2)

trinvh
trinvh

Reputation: 1590

You need jquery plugin to do that, or you can write your own.

I know a plugin is Multiple Select http://wenzhixin.net.cn/p/multiple-select/docs/#checkall-uncheckall

Upvotes: 0

Bogdan
Bogdan

Reputation: 44586

The Form::select() receives an array or collection which it then iterates over to generate all the <select> element's options. If you want to use checkboxes instead, you need to iterate manually to create each checkbox:

{!! Form::open(array('action' => 'UserController@visit_step_two', 'method' => 'post')); !!}
    @foreach ($servicesList as $value => $name)
        {!! Form::checkbox('services[]', $value, ['id' => 'service' . $value]); !!}
        {!! Form::label('service' . $value, $name) !!}
    @endforeach

    {!! Form::submit('Next'); !!}
{!! Form::close(); !!}

The above will create a form with a list of checkboxes that have labels attached. The id attribute is added so that you can also click on the label to select the associated checkbox, since the first parameter of Form::label() will be used to generate the for attribute of the <label> which is associated with a checkbox <input> field that has the same value for the id, and since an id has to be unique it is generated using the value like so 'service' . $value because all values should also be unique.

Upvotes: 1

Related Questions