2

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

7
  • You aren't sending the validation data to the client. Commented Aug 27, 2019 at 14:54
  • What is your route for submitting? Commented Aug 27, 2019 at 15:02
  • Route::resource('trainers', 'TrainerController'); Commented Aug 27, 2019 at 15:10
  • To add to what @Jerodev stated, you created the validation object, $validatedData, but you don't do anything with it. You just save the values passed in and always return 'Saved' Commented Aug 27, 2019 at 15:11
  • I'm following a tutorial that has exactly the same code as me, it works with an earlier version of Laravel Commented Aug 27, 2019 at 15:25

6 Answers 6

2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

same $this->validate
If you ever get Class ... not found, try fixing namespacing, so \Validator, or include at the top of your file with an use Validator; line.
use Validator; or use Illuminate\Support\Facades\Validator;
Integrity constraint violation: 1048 Column 'name' cannot be null (SQL: insert into trainers` (name, avatar, description, slug, updated_at, created_at) values (?, Sin Imagen.jpg, ?, ?, 2019-08-28 22:49:48, 2019-08-28 22:49:48))`
1

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

1 Comment

makes no difference, inspect with chrome and i dont no see error box. Use this code in blade and add {{ $errors }} , show [], is empty?
0

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

Comments

0

should add to method withError() As above

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

Comments

0
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 

Comments

0

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.

3 Comments

MAMP is only for Mac, isn't it?
for Windows too
So you recommend for Laravel better to use Mac then Windows :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.