Laravel - Show custom error in validation field - php

I want to show a custom error in validation field, but it always shows the default message.
This is my controller:
public function messages()
{
return [
'nombre.required' => 'Completá el nombre',
];
}
This is my blade template code:
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif

Related

Blade not showing messages on the view

I have a problem with showing error messages or session messages in laravel.
Comment the bootstrap cdn, but makes no differences.
have code like this, works correctly, refreshing the view, but not displaying errors:
Controller.php
return redirect()->route('trainers.show',[$trainer])->with('status','Entrenador actualizado correctamente');
blade.php
#if (session('status'))
<div class="alert alert-success">
{{session('status')}}
</div>
#endif
Controller.php
$validatedData = $request->validate([
'name' => 'required|max: 10',
'avatar' => 'required|image',
'slug' => 'required',
'description' => 'required',
]);
blade.php
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $message)
<li>{{ $message }}</li>
#endforeach
</ul>
</div>
#endif
Regarding the session status, you can use \Session::has('status') and \Session::get('status'), like so:
#if(\Session::has('status'))
<div class="alert alert-success">
{{ \Session::get('status') }}
</div>
#endif
And on your errors, you can also use has(), like so:
#if ($errors->has())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error}}</li>
#endforeach
</ul>
</div>
#endif

How can I get information about errors in laravel 6.0?

When I used Laravel 5.8 I could get information about errors like this :
#if(count($errors > 0))
<ul class="error-list" >
#foreach($errors->all() as $error)
<li class="error-list-element">{{$error}}</li>
#endforeach
</ul>
#endif
It worked completly fine.
Since Laravel 6.0 was released the code above results in error :
Object of class Illuminate\Support\ViewErrorBag could not be converted to int
So how can I get information about errors in Laravel 6.0?
Here is code :
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Hope this will help :)
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif

error to validate form in laravel

here's my error
my controller
this is my code at blade file
#if(count($errors))
<div class="alert alert-danger">
<ul>
#foreach($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
</div>
#endif
Follow this example
'name' => 'required|string|min:5|max:35'

Print error messages for multiple select

I am trying to print out error messages from a multiple select in Laravel:
#if($errors->has('interests')
<ul class="alert alert-danger">
#foreach($errors->get('interests') as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
#endif
Furthermore, my select has a property with name="interests[]". What do I do wrong?
If you do this kind of validation
'interests' => 'required|array|...'
and not target specific validation then, there will only be 1 error message for that field. So you can do this
#if($errors->has('interests'))
<ul class="alert alert-danger">
<li>{{ $errors->first('interests') }}</li>
</ul>
#endif

Display error of specific forms

I have two forms on a page (a blade template). Now I want to display form errors so I use:
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Now I have errors on both forms:
You can use this for laravel validation error. it throw specific error in specific field. You have to use this in every input control.
#if ($errors->has('input_name'))<p class="text-danger">
{!!$errors->first('input_name')!!}</p>#endif

Categories