Laravel Validations – Learn How To Use Laravel Validations

LARAVEL VALIDATIONS

If you have a post method of storing some user data which needs to validate their email id is unique never present before and check the name of the customer is not exceeds more than 256 characters and requirement of some field which is more necessary in form submission. We can apply validation rules  for this as given below using validate method in Laravel

public function storeUserData(Request $request)
{
    $validatedData = $request->validate([
        'name' => 'required|max:255',
        'email' => required|email|unique:users,email,
        'mobile' => 'required',
        'address' => 'required',

    ]);

if ($validatedData->fails()) {
    return view('users')->withErrors($validator)->withInput();
} else {
    return view('users')->with("message","success");
}
}

In users.blade.php catch the validation errors as given below.

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}li>
            @endforeach
        ul>
    div>
@endif

You may also like...

1 Response

  1. March 11, 2020

    […] may be granted abilities/scop to perform the necessary action which are allowed to perform and validate […]

Leave a Reply

Your email address will not be published. Required fields are marked *