How to set disabled select option in Laravel with Illuminate/Html
Asked Answered
C

7

7

I'm starting with Laravel and I'm using Illuminate/Html for making forms.

I want to add disabled attribute to the first option and I dont find the way to do it.

{!! Form::open(['url' => 'shelter/pets']) !!}
    <div class="form-group">
        {!! Form::label('pet_type','Type:') !!}
        {!! Form::select('pet_type', ['Select Type','dog', 'cat'], 0, ['class' => 'form-control']) !!}
    </div>
    <div class="form-group">
        {!! Form::submit('Add pet', null, ['class' => 'btn btn-primary form-control']) !!}
    </div>
{!! Form::close() !!}
Cardoza answered 21/4, 2015 at 11:31 Comment(1)
Hi javipedrera, did you get answer? please help me if you find answer. i face same problem :)June
J
7

Just pass the disabled in the options. Try with -

{!! Form::select('pet_type', ['Select Type','dog', 'cat'], 0, ['class' => 'form-control', 'disabled' => true]) !!}

You can do it manually looping through the array in php or by using jquery.

$('select.someclass option:first').attr('disabled', true);
Jasminejason answered 21/4, 2015 at 11:38 Comment(1)
Like I said to vanadium: That was what I did first but it disables all the select input, I want to disable only the first option.Cardoza
K
6

as the signature of the function here : vendor/laravelcollective/html/src/FormBuilder.php line #625 :

/**
     * Create a select box field.
     *
     * @param  string $name
     * @param  array  $list
     * @param  string|bool $selected
     * @param  array  $selectAttributes
     * @param  array  $optionsAttributes
     * @param  array  $optgroupsAttributes
     *
     * @return \Illuminate\Support\HtmlString
     */
    public function select(
        $name,
        $list = [],
        $selected = null,
        array $selectAttributes = [],
        array $optionsAttributes = [],
        array $optgroupsAttributes = []
    )

so you can use it like this:

{!! Form::select('pet_type', 
    ['Select Type','dog', 'cat'],
     0, //default selection
    ['class' => 'form-control'], //the select tag attributes
    [ 0 => [ "disabled" => true ] ] //list of option attrbitues (option value is the arrays key)
) !!}
Ketubim answered 18/5, 2021 at 10:57 Comment(0)
B
2

Looking through source, it looks like it's not possible. The <select> element is built up at https://github.com/illuminate/html/blob/master/FormBuilder.php#L532

The only args passed is the value, the name and the selected boolean. It looks like you have 2 solutions. Use javascript (argh), or use something like str_replace.

<?php

    $field = Form::select('pet_type', ['Select Type','dog', 'cat'], 0, ['class' => 'form-control']);

    // find value="Select Type" and replace with value="Select Type" dialled
    echo str_replace('value="Select Type"', 'value="Select Type" disabled', $field);

?>
Beaudry answered 21/4, 2015 at 12:11 Comment(0)
S
0

This might not be exactly what you are looking for but it will prevent user to select the first option but still be in the list.

Form::select supports A Grouped List and you can use it this way.

{!! Form::select('pet_type', ['Select Type' => ['dog', 'cat']], 0, ['class' => 'form-control']) !!}

More details: http://laravel.com/docs/4.2/html#drop-down-lists

Subduct answered 21/4, 2015 at 12:24 Comment(0)
P
0

i know this Post is really old but if you and others are still looking for a solution, i've found an easy way to do so:

Form::select('tableSeat', ['' => 'Bitte Auswählen', '1' => '1', '2' => '2'], null, ['class' => 'form-control form-dropdown'], ['' => ['disabled']])

we have a Select Element with options for 2 Table Seats. the last array indicates, that the field with the value '' gets disabled. you can do that too with your field 1 or 2 or whatever value you wanna disable

Pantin answered 18/4, 2023 at 8:0 Comment(0)
T
-1

The last array is used to form attributes of html tag, so you simple pass disabled into it:

    {!! Form::select('pet_type', ['Select Type','dog', 'cat'], 0, ['class' => 'form-control', 'disabled' => 'disabled']) !!}
Topminnow answered 21/4, 2015 at 11:38 Comment(1)
That was what I did first but it disables all the select input, I want to disable only the first option.Cardoza
P
-1

This could help you

Form::select('name_select', '', null, ['name' => '', 'id' => '', 'disabled' => 'disabled']) 
Pastern answered 1/3, 2019 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.