Laravel 8.12 does not show error messages

Viewed 2081

I don't understand why the error message is not displayed in form control with laravel. What is my error?

While I could get the "okey" message when I fulfill the conditions below, I could not clearly understand that the error message was not displayed because it did not meet the conditions.

"Information Is Beautiful When It Is Shared." routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Website\contact;

Route::get('/register', [contact::class, 'index'])->name('register');
Route::post('/register-submit', [contact::class, 'post'])->name('register.post');

contact.php

<?php

namespace App\Http\Controllers\Website;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class contact extends Controller
{
    public function index()
    {
        return view('Website.contact');
    }

    public function post(Request $request)
    {
        $request->validate([
            'name' => ['required', 'max:10'],
            'email' => ['required'],
        ]);

        echo "okey";
    }
}

contact.blade.php

 <div class="container pt-lg-5">

    @if ($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
    <form method="post" action="{{route('register.post')}}">
        @csrf
        <div class="form-group">
            <label for="exampleInputEmail1">name</label>
            <input type="text" class="form-control" name="name">
        </div>
        <div class="form-group">
            <label for="exampleInputPassword1">E-mail</label>
            <input type="email" class="form-control" name="email">
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
    </form>
</div>
Illuminate\Validation\Validator {#258 ▼
  #translator: Illuminate\Translation\Translator {#255 ▶}
  #container: Illuminate\Foundation\Application {#2 ▶}
  #presenceVerifier: Illuminate\Validation\DatabasePresenceVerifier {#259 ▶}
  #failedRules: array:2 [▼
    "name" => array:1 [▼
      "Required" => []
    ]
    "email" => array:1 [▼
      "Required" => []
    ]
  ]
  #excludeAttributes: []
  #messages: Illuminate\Support\MessageBag {#261 ▼
    #messages: array:2 [▼
      "name" => array:1 [▼
        0 => "The name field is required."
      ]
      "email" => array:1 [▼
        0 => "The email field is required."
      ]
    ]
    #format: ":message"
  }
  #data: array:3 [▶]
  #initialRules: array:2 [▶]
  #rules: array:2 [▼
    "name" => array:2 [▼
      0 => "required"
      1 => "max:10"
    ]
    "email" => array:1 [▼
      0 => "required"
    ]
  ]
  #currentRule: "required"
  #implicitAttributes: []
  #implicitAttributesFormatter: null
  #distinctValues: []
  #after: []
  +customMessages: []
  +fallbackMessages: []
  +customAttributes: []
  +customValues: []
  +extensions: []
  +replacers: []
  #fileRules: array:9 [▼
    0 => "Between"
    1 => "Dimensions"
    2 => "File"
    3 => "Image"
    4 => "Max"
    5 => "Mimes"
    6 => "Mimetypes"
    7 => "Min"
    8 => "Size"
  ]
  #implicitRules: array:10 [▶]
  #dependentRules: array:21 [▼
    0 => "After"
    1 => "AfterOrEqual"
    2 => "Before"
    3 => "BeforeOrEqual"
    4 => "Confirmed"
    5 => "Different"
    6 => "ExcludeIf"
    7 => "ExcludeUnless"
    8 => "ExcludeWithout"
    9 => "Gt"
    10 => "Gte"
    11 => "Lt"
    12 => "Lte"
    13 => "RequiredIf"
    14 => "RequiredUnless"
    15 => "RequiredWith"
    16 => "RequiredWithAll"
    17 => "RequiredWithout"
    18 => "RequiredWithoutAll"
    19 => "Same"
    20 => "Unique"
  ]
  #excludeRules: array:3 [▼
    0 => "ExcludeIf"
    1 => "ExcludeUnless"
    2 => "ExcludeWithout"
  ]
  #sizeRules: array:8 [▼
    0 => "Size"
    1 => "Between"
    2 => "Min"
    3 => "Max"
    4 => "Gt"
    5 => "Lt"
    6 => "Gte"
    7 => "Lte"
  ]
  #numericRules: array:2 [▼
    0 => "Numeric"
    1 => "Integer"
  ]
  #dotPlaceholder: "YEdPXjrZ4cLZZ3wV"
}
1 Answers

If you are willing for another way to solve your problem, or a quick solution you can always use Validator class directly, First add this to your controller, use Illuminate\Support\Facades\Validator; Then,

public function post(Request $request)
    {
        $validator = Validator::make($request->all(),[
            'name' => ['required', 'max:10'],
            'email' => ['required'],
        ]);
        
        if ($validator->fails()) {
            return redirect()->back()
                        ->withErrors($validator);
        }

        echo "okey";
    }
Related