therealbigpepe
therealbigpepe

Reputation: 1549

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

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() !!}

Upvotes: 7

Views: 24669

Answers (7)

EriJed
EriJed

Reputation: 3

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

Upvotes: 0

Abilogos
Abilogos

Reputation: 5030

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)
) !!}

Upvotes: 6

powerangelito
powerangelito

Reputation: 9

This could help you

Form::select('name_select', '', null, ['name' => '', 'id' => '', 'disabled' => 'disabled']) 

Upvotes: -1

mininoz
mininoz

Reputation: 5958

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

Upvotes: 0

dotty
dotty

Reputation: 41443

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);

?>

Upvotes: 2

Sougata Bose
Sougata Bose

Reputation: 31749

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);

Upvotes: 7

vanadium23
vanadium23

Reputation: 3586

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']) !!}

Upvotes: -1

Related Questions