Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincej's avatar
Level 15

How Best to Validate a Drop Down Menu ?

I have a drop down menu and the very first value is "Select"

When it comes to validation I want to make certain that the user has indeed selected something and not left the drop down as was.

I have looked at all the validation rules in the L5 docs and I can not find something which ignores a given value like "Select" and forces you to choose something else.

I thought perhaps the below would work - but Nope:

    'crew'=>'required_if:crew,Select',

Any ideas ?

Many thanks !

0 likes
15 replies
SachinAgarwal's avatar
Level 21

@vincej If the list of drop down comes from database, then you can do this

'dropdown' => 'exists:table,column';

if the list is hard coded, you can make a custom validation function which will make sure, the value is between the listed values.

2 likes
vincej's avatar
Level 15

Cheers you're right.. At the moment it is hard coded, but I guess the values should go into a DB - award for you !

mvtenorio's avatar

You could leave the value of your "Select" option as blank:

<option value="">Select</option>

That way, you can validate using only:

'crew' => 'required'
2 likes
vincej's avatar
Level 15

@mvtenorio

Thanks for that! I'm not sure that would work in Laravel forms. Check it out:

 <div class="col-md-2">{!! Form::label('crew', 'Crew:*') !!}</div>
   <div class="col-md-4">{!! Form::select('crew', ['s'=>'Select', 'nw'=>'North West', 'sw'=>'South West'], 's' ) !!}</div>

cheers !

bobbybouwmann's avatar

You can do it like this if you want @mvtenorio answer to work

<div class="col-md-4">
    {!! Form::select('crew', ['' => 'Select something...', 'nw' => 'North West', 'sw' => 'South West']) !!}
</div>

Note: by default it will select the first item in the array, so you don't have to set anything there ;)

1 like
vincej's avatar
Level 15

@blackbird

Note: by default it will select the first item in the array, so you don't have to set anything there ;)

Cool - had not thought of that. Never the less, the crew locations should go into a Db.

Thanks ! Bedankt !

mvtenorio's avatar

It can still work if you get the locations from the DB. In that case, it could be something like this:

 {!! Form::select('crew', ['' => 'Select something...'] + $locations) !!}

$locations being an array with the data from the database.

vincej's avatar
Level 15

@SachinAgarwal @blackbird @mvtenorio

Guys - I don't get it. I'm getting a bad method call error. I'm obviously doing something basic which is wrong. I created a table called "crews" in my DB "auburntree". The field name is called "crew" and I have some values in the DB. So, in my validation array I do:

field name=>databasename:tablename

    'crew'=>'auburntree:crews',

If I do:

value=>databasename:tablename

I don't get an error - but I could 10 values or more. It does not make sense that I should have to itemise all these.

Many Thanks !

vincej's avatar
Level 15

@SachinAgarwal Many thanks ! I got it working, but that was seriously painful. I found the docs very unclear.

Cheers !

bobbybouwmann's avatar

The documentation is pretty clear to me

Specifying A Custom Column Name

'state' => 'exists:states,abbreviation'

vincej's avatar
Level 15

Hi @blackbird

Well it's obvious now, but it certainly did not jump off the page for me. The problem I had was, that the example does not follow the given protocol.

exists:table,column

'state' => 'exists:states'

I would have worked it out, had the protocol read something like:

'field_name'=>'exists:table,column'

with an example like:

'state' =>'exists:location,states'

Please or to participate in this conversation.