8

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.

7
  • 1
    show your HTML code Commented Sep 25, 2017 at 12:45
  • It won't help because it is an expected behavior that null inputs are not included in the request body. It happens for radio inputs and checkboxes as well. Commented Sep 25, 2017 at 12:47
  • show your blade template. Commented Sep 25, 2017 at 12:51
  • add value="" to <option disabled selected>Please select...</option> and Your rule should looks like 'select_input' => 'required|in:val1,val2' ;) Commented Sep 25, 2017 at 13:02
  • @Maraboc I've tried that before, but seems that any validation is ignored for that field because it is not present in request parameters. Seems that only the present validation method from Laravel 5.2 would work, but unfortunately I can't upgrade the project to a newer version because there are a lot of things I would need to change... Commented Sep 25, 2017 at 13:06

3 Answers 3

9

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",
Sign up to request clarification or add additional context in comments.

4 Comments

is your value's of options are string? If so then use required|string
Yes, the values are strings but still seems that the validate method ignores any input that is not included in the request.
change your html as I added to my answer
It works indeed without the disabled attribute and with an empty value. :)
1

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>

2 Comments

I forgot to mention that I use Laravel 5.1 which doesn't include this validation rule, unfortunately.
Share you select box html in your question
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.