Home > Backend Development > PHP Tutorial > Why Aren't My Laravel 5.2 Validation Errors Showing in My Blade View?

Why Aren't My Laravel 5.2 Validation Errors Showing in My Blade View?

DDD
Release: 2024-12-05 04:04:09
Original
1037 people have browsed it

Why Aren't My Laravel 5.2 Validation Errors Showing in My Blade View?

Laravel 5.2 Validation Errors Not Displaying in Blade

Problem:

Validation errors fail to appear in the blade view when a user enters invalid input during a form submission.

Controller:

public function saveUser(Request $request) {
    $this->validate($request, [
        'name' => 'required|max:120',
        'email' => 'required|email|unique:users',
        'phone' => 'required|min:11|numeric',
        'course_id' => 'required'
    ]);

    $user = new User();
    $user->name = $request->input(['name']);
    $user->email = $request->input(['email']);
    $user->phone = $request->input(['phone']);
    $user->date = date('Y-m-d');
    $user->completed_status = '0';
    $user->course_id = $request->input(['course_id']);
    $user->save();
    return redirect('success');
}
Copy after login

Blade View:

@if(count($errors) > 0)
    <div>
Copy after login

Solution:

In Laravel 5.2.27 or higher, the web middleware is now automatically applied to all routes within routes.php. Manually adding it can cause issues.

To resolve this issue, remove the web middleware from the route group in RouteServiceProvider.php:

protected function mapWebRoutes(Router $router)
{
    $router->group([
        'namespace' => $this->namespace,
        // Remove the 'web' middleware
    ], function ($router) {
        require app_path('Http/routes.php');
    });
}
Copy after login

The above is the detailed content of Why Aren't My Laravel 5.2 Validation Errors Showing in My Blade View?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template