Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 617
Eseth Avatar asked Sep 25 '17 12:09

Eseth


People also ask

Why is my form not being validated in Laravel?

} 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.

How to create a custom Validation rule in Laravel?

One method of registering custom validation rules is using rule objects. To generate a new rule object, you may use the make:rule Artisan command. Let's use this command to generate a rule that verifies a string is uppercase. Laravel will place the new rule in the app/Rules directory.

How to return NULL if no old input exists in Laravel?

If no old input exists for the given field, null will be returned: By default, Laravel includes the TrimStrings and ConvertEmptyStringsToNull middleware in your application's global middleware stack. These middleware are listed in the stack by the App\Http\Kernel class.

What is the default email validation in Laravel?

The filter validator, which uses PHP's filter_var function, ships with Laravel and was Laravel's default email validation behavior prior to Laravel version 5.8. The dns and spoof validators require the PHP intl extension. ends_with: foo, bar ,...


Video Answer


3 Answers

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",
like image 78
Saroj Avatar answered Nov 09 '22 20:11

Saroj


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>
like image 43
Morteza Rajabi Avatar answered Nov 09 '22 21:11

Morteza Rajabi


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.

like image 35
Oluwatobi Samuel Omisakin Avatar answered Nov 09 '22 20:11

Oluwatobi Samuel Omisakin