0

I am trying to display form errors in case form validation fails. Everything works fine and form is validated correctly but it does not display form errors in view. Every time an empty array is returned as errors.

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class HomeController extends Controller
{
    public function storeProjectDetails(Request $request)
   {
    $messages = [
        'title.required' => 'Please enter trip title',
        'title.max' => 'Only 254 characters are allowed as trip title',
        'startDate.required' => 'Please enter trip start date',
        'startDate.date' => 'Only date formats are allowed as start date',
        'endDate.required' => 'Please enter trip end date',
        'endDate.date' => 'Only date formats are allowed as end date',
    ];

    $this->validate($request,[
        'title' => 'required|string|max:254',
        'startDate' => 'required|date',
        'endDate' => 'required|date',
    ]);

  }
}

View:

  print_r($errors->all());
aishazafar
  • 1,024
  • 3
  • 15
  • 35

4 Answers4

0

used this

@if($errors->has())
   @foreach ($errors->all() as $error)
      <div>{{ $error }}</div>
  @endforeach
@endif
Usman Jdn
  • 807
  • 8
  • 21
0

First you need to check for errors using an if condition then you need to print the errors using a loop as given below

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

In your case, you are missing to return the errors array in your controller. Try the validation check given below.

$this->validate($request,[
        'title' => 'required|string|max:254',
        'startDate' => 'required|date',
        'endDate' => 'required|date',
    ], $messages);
Bonish Koirala
  • 641
  • 5
  • 16
  • 1
    If we don't return messages with validate function it displays default messages for required and other rules. So this is not the issue. There is something else due to that errors are not being displayed. – aishazafar Nov 20 '18 at 08:11
  • did you try displaying errors as shown in my answer? – Bonish Koirala Nov 20 '18 at 08:13
  • yes. That's only a way to display errors. Which can be done with print_r too. – aishazafar Nov 20 '18 at 08:16
  • How did you know that its validating correctly yet you didn't able to see the error? – Jesus Erwin Suarez Nov 20 '18 at 08:23
  • @JesusErwinSuarez after your question I modified the code and checked again thinking there may be my mistake. Even only with required rule it does not display any error. – aishazafar Nov 20 '18 at 08:48
  • can you show your view code and routes for us to understand the real issue? with the information you've provided, this is the only solution i can think of – Bonish Koirala Nov 20 '18 at 09:33
0

You are building the messages array in a wrong format, it should look like this:

$messages = [
    'required' => 'Please enter :attribute',
    'date' => 'Only date formats are allowed as :attribute',
    ...
];
Arthur Samarcos
  • 3,262
  • 22
  • 25
0

Well, for others with the same issue, let me share my solution initially shared here: https://github.com/laravel/framework/issues/27729#issuecomment-1442976348

There are a lot of similar related issues what turns that very tricky.

I think here is the right thread to share what I found.


For instance, I'm working in a legacy system that was updated from Laravel 5.2 to 5.5 a few months ago, so keep in mind that your issue might be different.

Initially I thought that it could be due the settings of the middlewares, more about that in the links below:


After a few hours without success or anything really helpful, I decided to dig the old code with Laravel 5.2.

Well, they changed the behavior of ValidatesRequests trait. Not only, but mainly the function validate:

// 5.5
    public function validate(Request $request, array $rules,
                             array $messages = [], array $customAttributes = [])
    {
        $this->getValidationFactory()
             ->make($request->all(), $rules, $messages, $customAttributes)
             ->validate();

        return $this->extractInputFromRules($request, $rules);
    }
// 5.2 - I didn't look carefully, but it seems that wasn't changed until 5.4
    public function validate(Request $request, array $rules, array $messages = [], array $customAttributes = [])
    {
        $validator = $this->getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes);

        if ($validator->fails()) {
            $this->throwValidationException($request, $validator);
        }
    }

After figured out that, I went back to the docs and didn't find anything related to that.

image

Anyway, it seems that after 5.5 if you want to send the errors and the inputs to the view, instead of only do return back();, you will need to use something like return back()->withInput()->withErrors($exception->validator ?? '');.

Alternatively,you can create a custom trait using the old code from 5.2 (perhaps combine both) and override the uses of ValidatesRequests to CustomValidatesRequests.

After recover the trait from 5.2, the error messages came back to the system. :smiley:

Despite of that, keep in mind this might cause other issues, if possible it's better to use ->withInput()->withErrors($exception->validator ?? '');.

FabianoLothor
  • 2,752
  • 4
  • 25
  • 39