mardi 12 mars 2019

Laravel 5.8: Validation multiple inputs

I have a form with 3 inputs and I want to check the following conditions:

  • All inputs are integers and they are required.
  • We do a math operation with all the numbers and we get if the operation was successfull or not.
    • Success: we redirect the user to a success page.
    • No success: we show a error message to the user with a message explaining him that the numbers aren't valid.

I resolved this with these lines:

function formAction(Request $request) {
    $this->validate($request, [
        'number1' => 'integer|required',
        'number2' => 'integer|required',
        'number3' => 'integer|required',
    ]);

    $numbers = $request->all();
    $isValid = MyOwnClass::checkMathOperation($numbers);

    if($isValid) {
        return redirect()->route('success');
    } else {
        $request->session()->flash('error', 'The numbers are not valid.');
        return back();
    }
}

What I looking for:

  • When MyOwnClass::checkMathOperation($numbers) is false:
    • To highlight number1, number2 and number3 inputs.
    • To show a unique custom error message
    • To hide the number1, number2 and number3 input error messages.

How can I do that with validators?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire