Borut Flis
Borut Flis

Reputation: 16375

Displaying errors with Laravel Form validation

I am learning Laravel. I am doing Form Validation at the moment. In the documentation it is said the variable $errors is flashed to the session and is always available. I get an exception because an undefined variable. I only pasted the sample code from the documentation:

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

Error message:

ErrorException in 318c473e4384f7c25db0019a770ee937b30041d1.php line 41: Undefined variable: errors (View: C:\xampp\htdocs\NightClubs\resources\views\add.blade.php)

This are the validation rules in the controller:

$this->validate($request, [
        'youtube' => 'required|url',
        'coordinatex' => 'required|between:-180,180',
        'coordinatey' => 'required|between:-90,90',
        'nameofclub' => 'required'
]);

Upvotes: 0

Views: 99

Answers (2)

jemz
jemz

Reputation: 5123

I assume you have validation rules in your controller.

Try this in your view

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

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

Try to use the group for routes where you want to use $errors variable:

Route::group(['middleware' => ['web']], function () {
    // Your routes
    // Your routes
}

Upvotes: 1

Related Questions