4

How To Show Validation Errors In Laravel Views

 3 years ago
source link: https://www.codewall.co.uk/how-to-show-validation-errors-in-laravel-views/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@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 –

validation errors laravel view

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK