Blade not showing messages on the view - php

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

Related

The Validation not working , the error message wont show up Laravel 8

Im a laravel beginner, also my first time to use a framework i just learned laravel for 5 days,so i don't understand much about this framework, im using the latest laravel 8 i have a problem with my project, when trying to validate the data from the form.
i dont know this method is working or not (Probably not)
$request->validate([
'name' => 'required',
'icons' => 'required',
]);
when i trying to get the error message {{$error}} , only showing [] in my view.
<form action="{{ route('poseicon.store') }}" method="post" enctype="multipart/form-data">
#csrf
<div class="display">
<label>name:</label>
<input type="text" name="name" id="name">
<label>icons:</label>
<input type="file" name="icons" id="icons" class="">
{{$errors}}
<input type="submit" value="submit">
</div>
</form>
this is my controller
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'icons' => 'required',
]);
//this working correctly when validate method above deleted
$file = $request->file('icons');
$filename = 'pose-photo-' . time() . '.' . $file->getClientOriginalExtension();
$path = $file->storeAs('img', $filename);
//this also working correctly when validate method above deleted
Poseicon::create([
'name' => $request->name,
'icons' => $filename
]);
return redirect('/poseicon');
}
i already try all the documentation said but still not working
As laravel doc says, you should use one of #error directive or loop over $errors
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
OR
#error('title')
<div class="alert alert-danger">{{ $message }}</div>
#enderror
Simply put this code where you want to show error message. Laravel provide this error code.
In your view page you put this.
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
In your controller You simply declare which field required.
$request->validate([
'name' => 'required',
'icons' => 'required',
]);
Try this.
controller
use Illuminate\Support\Facades\Validator;
$validatedData = Validator::make($request->all(), [
'name' => 'required',
'icons' => 'required',
])->validate();
$name = $validatedData['name'];
$icons = $validatedData['icons'];
blade file put this above your form
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif

Laravel - Show custom error in validation field

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

Why won't my success message that's coming from the controller appear in the view?

I'd like to know why my bootstrap success message that's coming from controller (PostsController.php) won't appear in the view (messages.blade.php)?
I see the Bootsrap success green banner coming about but not the message itself. What could be the reason why?
Here's messages.blade.php:
#if(count($errors) > 0)
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
#endforeach
#endif
#if(session('success'))
<div class="alert alert-success">
{{session('session')}}
</div>
#endif
#if(session('error'))
<div class="alert alert-danger">
{{session('error')}}
</div>
#endif
Here's PostController.php:
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required'
]);
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
return redirect('/posts')->with('success', 'Post Created');
}
#if(session('success'))
...
{{session('session')}}
...
#endif
I think you want session('success') inside the div?

No message error occurs MethodNotAllowedHttpException Laravel

I get no message error after click on submit. Here is my view, which is shown correctly.
{!! Form::open(array('route'=>'store.invitation')) !!}
<div class="form-group">
<input type="hidden" name="iduser" value="{{ $user->id }} "/>
<select class="form-control" name="idgroup">
#foreach ($groups as $group)
<option value="{{ $group->id }}">{{ $group->name }}</option>
#endforeach
</select>
{{ csrf_field() }}
</div>
<div>
{{Form::submit('User einladen',['class' => 'btn btn-primary'])}}
</div>
{!! Form::close() !!}
This is my routes. The last one belongs to this view store.invitation.
Route::get('invitation', 'UsernameController#search')->name('search.username');
Route::post('invitation/result', 'UsernameController#result')->name('result.username');
Route::post('invitation/store', 'InvitationController#store')->name('store.invitation');
And this is my function...
public function store(Request $request)
{
$request->validate([
'idgroup' => [
'required', 'numeric', new AdminGroupRequest, new MaxInvitationAdmin
],
'iduser' => [
'required', 'numeric', new AlreadyInvitation, new AlreadyPartOfGroup, new MaxInvitationUser
]
]);
$iduser = $request->iduser;
$idgroup = $request->idgroup;
$i = new Invitation;
$i->idgroup = $idgroup;
$i->iduser = $iduser;
$i->active = 1;
$i->save();
$username = User::where('id', $iduser)->select('name')->first();
$groupname = Group::where('id', $idgroup)->select('name')->first();
return redirect()->action(
'UsernameController#search')->with('success', 'Sie haben den User ' .$username->name. ' in die Gruppe ' .$groupname->name. ' eingeladen.');
}
EDIT: Problem is the validation. But in my view I have
#if(#count(errors > 0))
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
#endforeach
#endif
Instead of:
#if(#count(errors > 0))
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
#endforeach
#endif
you should use
#if($errors->any())
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
#endforeach
#endif
or because you don't wrap errors in any container you could just use:
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
#endforeach

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'

Categories