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
DDDOriginal
2024-12-05 04:04:091030browse

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');
}

Blade View:

@if(count($errors) > 0)
    <div>

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');
    });
}

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!

Statement:
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