Why laravel validation don't displaying errors

↘锁芯ラ 提交于 2020-01-24 20:30:08

问题


I'm trying to validate a request from form and appear a problem, not showing error messages on the view. The validation succeeds, refreshing the view but not displaying errors. Apparently $errors is always empty I tested it on fresh installation.

Laravel Version: 5.8.33 PHP Version: 7.3.8 Database Driver & Version: mysql 5.7.22

Form is this

@section('content')    
    @if ($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $message)
                    <li>{{ $message }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <form class="form-group" method="POST" action="/trainers" enctype="multipart/form-data">
        @csrf   

        <div class="form-group">
            <label for="">Nombre</label>
            <input type="text" name="name" value="@isset($trainer->name){{$trainer->name}}@endisset" class="form-control">
        </div>

        <div class="form-group">
            <label for="">Slug</label>
            <input type="text" name="slug" value="@isset($trainer->slug){{$trainer->slug}}@endisset" class="form-control">
        </div>

        <div class="form-group">
            <label for="">Descripción</label>
            <input type="text" name="description" value="@isset($trainer->description){{$trainer->description}}@endisset" class="form-control">
        </div>

        <div class="form-group">
            <label for="">Avatar</label>
            <input type="file" name="avatar">
        </div>

        <button type="submit" class="btn btn-primary">Guardar</button>
        </form>
@endsection

The store function with validation is this

public function store(Request $request)
    {
        $validatedData = $request->validate([
            'name' => 'required|max: 10',
            'avatar' => 'required|image',
            'slug' => 'required',
            'description' => 'required',
        ]);

        $trainer = new Trainer();
        $file = $request->file('avatar');

        if ($file != "") {
            $name = time().$file->getClientOriginalName();
            $file->move(public_path().'/images/', $name);
            }
            else{
                $name = "Sin Imagen.jpg";
                }
        // return $name; //Ver nombre archivo
        $trainer->name = $request->input('name');
        $trainer->avatar = $name;
        $trainer->description = $request->input('description');
        $trainer->slug = $request->input('slug');
        $trainer->save();

        return 'Saved';
    }

UPDATE Add {{ $errors }} in blade and return [], $errors is empty


回答1:


Instead of using $request->validate edit your code and Use $this->validateor this code instead:

$messages = [
              'description.required' => 'description is required',
       ];
        $validator = Validator::make($request->all(), [
            'name' => 'required|max: 10',
            'avatar' => 'required|image',
            'slug' => 'required',
            'description' => 'required',
        ],$messages);



回答2:


if page refresh means error object not empty...and your validation code in controller work so check html code with inspect element in browser.maybe error box is hidden or for test it you can use this code in your blade

  @if ($errors->any())
    @dd($errors){{-- for check error bag --}}
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $message)
                <li>{{ $message }}</li>
            @endforeach
        </ul>
    </div>
@endif



回答3:


try this

@if(session('success_message'))
            <div class="alert alert-success alert-dismissible" role="alert">
              <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span> </button>
              <strong>Successful!</strong> {{ session('success_message') }}</div>
            @endif
            @if(session('error_message'))
            <div class="alert alert-danger alert-dismissible" role="alert">
              <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span> </button>
              <strong>Error!</strong> {{ session('error_message') }}</div>
            @endif

        @if (count($errors) > 0)
                        <div class="alert alert-danger alert-dismissible" role="alert">
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span>
                            </button>
                            <strong>Error!</strong> 
                            @foreach ($errors->all() as $error)
                                <p>{{ $error }}</p>
                            @endforeach
                        </div>
            @endif



回答4:


should add to method withError() As above

if ($validator->fails())
{
   return redirect()
    ->back()
    ->withErrors($validator)
    ->withInput();
}



回答5:


I hope this will help you 

$validator = Validator::make($request->all(), [
    'amount' => 'required|amount|numeric|min:25'
])->validate();

if (isset($validator) && $validator->fails()) {
    return redirect()->route('investment')
        ->withErrors($validator)
        ->withInput();
}

in your blade file add below code
@if (count($errors) > 0)
<div class = "alert alert-danger">
  <ul>
     @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
     @endforeach
  </ul>
 </div>
@endif 



回答6:


It's strange but solve the problem by changing local server. I used XAMPP and now I use MAMP and everything works perfect. If you are working locally with Laravel I recommend not using XAMPP.



来源:https://stackoverflow.com/questions/57677363/why-laravel-validation-dont-displaying-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!