Eseth
Eseth

Reputation: 579

Laravel ignores select input validation if no option is selected

I have a form with some fields which I want to validate using Laravel's validate() method.

public function postSomething(Request $req) {

    ...

    $this->validate($req, [
        'text_input' => 'required',
        'select_input' => 'required'
    ]);

    ...

}

The issue is that if the form is submitted without selecting an option from the select input it is ignored in the request and Laravel doesn't validate it despite the fact that it is added to the ruleset with the required validation rule. Empty text inputs are being validated correctly.

+request: ParameterBag {#42 ▼
  #parameters: array:1 [▼
    "text_input" => ""
    "_token" => "TCDqEi2dHVQfmc9HdNf8ju1ofdUQS6MtDBpUMkl7"
  ]
}

As you can see, the select_input is missing from request parameters if it was left empty.

Here is the HTML code for my select input:

<select class="form-control" name="select_input">
    <option disabled selected>Please select...</option>
    <option value="val1">Value 1</option>
    <option value="val2">Value 2</option>
</select>

Is there a way to make the validation work for all fields from the ruleset even if some of them are not present in the request?

From Laravel 5.1 validation documentation:

required

The field under validation must be present in the input data and not empty. A field is considered "empty" is one of the following conditions are true: The value is null. The value is an empty string. The value is an empty array or empty Countable object. The value is an uploaded file with no path.

P.S. I'm using Laravel 5.1, so present method is not available.

Upvotes: 8

Views: 17939

Answers (3)

omitobi
omitobi

Reputation: 7334

There are few options I can recommend:

  • Manually validate the request without using the validation extended in the Controller, I.e:

    //validator FACADE
    $ validator = Validator::make ($request->all(), [
        // rules here
    ]);
    

    By this you can monitor which fields are passed and which one are not passed.

  • Secondly, set a default value for the select list and check that value when you are validating in the Controller, that is, if you have this default value then nothing is selected. You definitely will have only the fields submitted in your Controller.

Upvotes: 1

Saroj
Saroj

Reputation: 1373

Your html should look like this

<select class="form-control" name="select_input">
    <option value="" selected >Please select...</option>
    <option value="val1">Value 1</option>
    <option value="val2">Value 2</option>
</select>

$this->validate($req, [
        'text_input' => 'required',
        'select_input' => 'required',
    ]);

If your select box values are integer then you can use required with integer like

$this->validate($req, [
    'text_input' => 'required',
    'select_input' => 'required|integer',
]);

Or if you have limited options for that select box then you can use

'select_input' => "required|in:val1,val2,val3",

Upvotes: 9

Morteza Rajabi
Morteza Rajabi

Reputation: 2913

You made it's option disabled, so it won't send anything through your form.

Change your select box to

<select class="form-control" name="select_input">
    <option value="">Please select...</option>
    <option value="val1">Value 1</option>
    <option value="val2">Value 2</option>
</select>

Upvotes: 1

Related Questions