peke_peke
peke_peke

Reputation: 551

How to Validate a Html select Element with Laravel 5?

I've got a section list in my viewlike this:

        <select name="departments">
                        <option value="1">support 1</option>
                        <option value="2">support 2</option>               
         </select>

Now I would like to check if the selected option value is in my database, so users cannot sent an incorrect number of support department.

How can I access the "value" in my validator?

$validator = Validator::make($request->all(), [
    'how to access the value?!' => 'required|exists:departments,id',
    ]);

Upvotes: 0

Views: 1028

Answers (1)

Ohgodwhy
Ohgodwhy

Reputation: 50787

I think there's a misunderstanding of what's going on. It's not the value you need to access, you need to tie it into the name of the element. Let's see what that looks like:

'departments' => 'required|exists:departments,id'

This will make sure the selected value exists in the departments table on the id column.

Upvotes: 1

Related Questions