imurangel
imurangel

Reputation: 53

Laravel - Validation Message not showing on blade

I'm having trouble on showing the validation error messages. I tried the other similar questions but doesn't work for me. Please help me.

This is my Controller.blade.php

    public function store(Request $request)
    {
        $this->validate($request,[
            'surname'=>['required', 'string', 'max:255'],
            'firstname'=>['required', 'string', 'max:255'],
            'birthday'=>'required',
            'pofbirth'=>['required', 'string', 'max:255'],
            'gender'=>['required', 'numeric'],
            'address'=>['required', 'string', 'max:255'],
            'city'=>['required', 'string', 'max:255'],
            'district'=>['required', 'string', 'max:255'],
            'cap'=>['required', 'string', 'max:255'],
            'cstatus'=>['required', 'string', 'max:255'],
            'conjugated'=>['required', 'string', 'max:255'],
            'cellphone'=>['required', 'numeric'],
            'email'=>['required', 'string', 'email', 'max:255'],
            ]);

This is my message.blade.php

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

@if (session()->has('message'))

    <p class="alert alert-success">{{ session('message') }}</p>


@endif

This is my blade.php

    @include('includes.navbar')
    
    <div class="col" style="background-color: #ffffff;">
        @include('includes.message')
        <form  method="POST" action="{{ route('app.store')}}" enctype="multipart/form-data">
            {{ csrf_field() }}
        <div class="container shadow" style="background-color: rgb(248,249,250);">
            <div class="row">
                <div class="col">
...
...
...

Thank you so much!

Upvotes: 0

Views: 6382

Answers (1)

STA
STA

Reputation: 34688

In your method, you have no any session key named message , but there are no any key named message in your validation :

@if (session()->has('message'))
    <p class="alert alert-success">{{ session('message') }}</p>
@endif

It would be firstname, surname, gender, age etc (which key you are using in validation).
Like this :

@if($errors->has('firstname'))
    <p class="alert alert-success">{{ $errors->first('firstname') }}</p>
@endif

Get all error message :

@if ($errors->any())
   @foreach ($errors->all() as $error)
      <div>{{$error}}</div>
   @endforeach
@endif

Change your validation in this way :

$validatedData = $request->validate([
        'surname' => 'required|string|max:255',
        'firstname' => 'required|string|max:255',
        'birthday' => 'required',
        'pofbirth' => 'required|string|max:255',
        'gender' => 'required|numeric',
        'address' => 'required|string|max:255',
        'city' => 'required|string|max:255',
        'district' => 'required|string|max:255',
        'cap' => 'required|string|max:255',
        'cstatus' => 'required|string|max:255',
        'conjugated' => 'required|string|max:255',
        'cellphone' => 'required|numeric',
        'email' => 'required|string|email|max:255',
    ]);

Upvotes: 2

Related Questions