Summer Sale! All accounts are 50% off this week.

kay899's avatar

Form::select() with first option disabled?

Hi,

I need to make a Form::select() where the first option is selected and disabled, like here in line 4:

http://laravel.io/bin/qQQxW#

How can I do that?

Thanks kay899

0 likes
6 replies
pmall's avatar

You cant add disabled option with form::select.

Just make it required it in your rules so empty value is not accepted.

1 like
pmall's avatar

I had understood. You can't add disabled attribute to an option with form::select(). If you want to do it you have to write the plain html.

davidfaux's avatar

How I usually do this is:

    {!! Form::select('month', [null => 'Month'] + $months, null, ['class' => 'valid', 'required']) !!}
    //$month is an array passed from the controller ['January' => 1, 'etc']

Then use validation to ensure that a month has been selected. As @pmall has mentioned you cannot pass a disabled attribute from within the array to the view. If you really needed this you'll have to create it manually using a loop that has a check to see if it is the first option and if so to add the disabled attribute to it.

pmall's avatar

By the way, a selected & disabled option is a non sense.

Please or to participate in this conversation.