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>
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';
}
By convention, $request->validate([]) will return the errors to the view. However you are assigning it to a variable ($validatedData) so laravel thinks that you are treating the data anyways.
Just remove the assigment of $validatedData and laravel will return it for you:
public function store(Request $request)
{
$request->validate([
'name' => 'required|max: 10',
'avatar' => 'required|image',
'slug' => 'required',
'description' => 'required',
]);
...
}
By convention, $request->validate([]) will return the errors to the view. However you are assigning it to a variable ($validatedData) so laravel thinks that you are treating the data anyways.
thanks but makes no difference, only refresh the view
try this
$this->validate($request, [ 'name' => 'required|max: 10', 'avatar' => 'required|image', 'slug' => 'required', 'description' => 'required', ]);
$this->validate($request, [ 'name' => 'required|max: 10', 'avatar' => 'required|image', 'slug' => 'required', 'description' => 'required', ]);
try this but return this error:
Symfony\Component\Debug\Exception\FatalThrowableError thrown with message "Argument 1 passed to LaraDex\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request, array given, called in /Users/jerefigueroa/.bitnami/stackman/machines/xampp/volumes/root/htdocs/laravel/cursoLaravel/app/Http/Controllers/TrainerController.php on line 63"
Please make sure that the form action is pertaining to the controller on which your store method is defined.
that's ok
can you please do {{ $errrors ? dd($errors) : null }}
And post here what you got
i got this
but this way:
@if ($errors->any())
{{ $errors ? dd($errors) : null }}
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $message)
<li>{{ $message }}</li>
@endforeach
</ul>
</div>
@endif
makes no difference, refresh the view
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community