Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How To Show Validation Errors In Laravel Views

Validation feedback messages are an invaluable addition to any CRUD style application. If your app allows users to create records that are stored in your database, then you will of course want to validate them before saving to the database. Laravel comes with methods to handle this and it comes with Laravel blade syntax that will display any Validation errors to the user.

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

Step 1

Ensure proper validation checks are being executed against submitted request data in the controller.

In this controller’s create function, the data for a product is expected, a string name and a double weight.

PHP

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

        return view('products.index');

    }

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

PHP

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

        return view('products.index');

    }

Now the controller uses $request->validate() to check the specified rules 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.

Step 2

Now that the controller is validating the request, all you need to is add the following code to your view file, I like to make sure this is above the form, but it’s completely up to you.

PHP

@if ($errors->any())
    
    @foreach ($errors->all() as $error)
  • {{ $error }}
  • @endforeach
@endif

Now if you go ahead and submit your form without the required fields in the controllers’ validation rules, you will be presented with something like the following –

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 (or layout file) to have this functionality site-wide.

The post How To Show Validation Errors In Laravel Views appeared first on Code Wall.



This post first appeared on Code Wall - Web Development & Programming, please read the originial post: here

Share the post

How To Show Validation Errors In Laravel Views

×

Subscribe to Code Wall - Web Development & Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×