lundi 25 septembre 2017

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.

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?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire