How To Show Validation Errors In Laravel

How To Show Validation Errors In Laravel

In this tutorial, we will learn How To Show Validation Errors In Laravel. Submit a form and leave a field blank, then it is not submitted and gives an alert right there.

When we make the form, there are many fields in which if we leave one field, it gives an alert, it ensures us that we have to fill this field and not leave it empty which gives an error if left empty.

Laravel comes with methods to handle this error and it comes with Laravel blade syntax that will display any validation errors in laravel to the user.

There are two parts to ensuring that validation errors checks are carried out and reported into the view in your form, make your way through the following steps to add laravel validation errors to your view.

Step 1: Create Controller Show Validation Errors In Laravel

In this step, we will create a function which will be in the controller, in that we will define how to make the error show in laravel in this, we will add the field like name and email.

In this controller create a function, the data for a product is expected, a string name, and a string email.

public function store(Request $request)
    {
        
        $product = new Product([
            "name" => $request->get('name'),
            "email" => $request->get('email')
        ]);
        
        $product->save();

        return view('products.index');

    }

As you can see, the controller function handles data being submitted and saves it to the database store form error, but zero checks are carried out. We can adjust this controller to validate error the inputs like so –

public function store(Request $request)
    {
        //**** Validate the inputs here
        $request->validate([
            'name' => 'required',
            'email' => 'required',
        ]);
        //****
        
        $product = new Product([
            "name" => $request->get('name'),
            "email" => $request->get('email')
        ]);
        
        $product->save();

        return view('products.index');

    }

Now the controller uses $request->validate() to check the specified rules form validation against the data submitted in the request. If all is good, it will continue and create a new Product record, if not, it will exit, redirecting back to the create view passing in $errors and print error in the form.

Step 2: Show Laravel Errors In View

In this step that the controller is validating the request, all you need to is add the following code to your view file (Blade file), I like to make sure this is above the form error in laravel, but it’s completely up to you.

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

Conclusion

Now if you go ahead and submit your form without the required fields in the controller’s validation rules (Go to this URL). And that is it, ensure that both a validation method is set up within your controller and the error loop is added to your view (blade file).

Leave a Reply

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