Laravel is displaying error message before form submit - php

in my last question i was asking for help with single error message displaying after incorrectly filled form instead of error for every incorrect input. They were wery helpfull and I received this piece of code:
#if ($errors)
<span class='help-block'>
<strong>{{ "There are errors" }}</strong>
</span>
#endif
and the second answer was
#if (!empty($errors))
<span class='help-block'>
<strong>{{ "Some input field is not properly filled" }}</strong>
</span>
#endif
I thought that the above will be displayed only after form submiting and if Laravel will find any errors. The problem is that it's always displaying error message like in the screenshot under:
Does anyone knows the solution? Many Thanks.

$errors variable is always set. It's a message bag that might or might not contain messages.
You could check like this:
#if ($errors->count() > 0)
<span class='help-block'>
<strong>{{ "Some input field is not properly filled" }}</strong>
</span>
#endif
#if (!$errors->isEmpty()) should also work.

So if I understood it correctly, you want to show the success or error message only after submitting your form.
If that is the case following should work:
You need to add / change following in your Controller function:
$request->session()->flash('success', 'Success!');
return redirect()->route("yourstuff.index");
In your blade.php you return that session with it message like this
#if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
#endif
#if (session('error'))
<div class="alert alert-success">
{{ session('error') }}
</div>
#endif

The problem is solved, i received two good answers but i choosed simplier one.
In order to return single error I used the code proposed by user:
devk
#if ($errors->count() > 0)
<span class='help-block'>
<strong>{{ "Some input field is not properly filled" }}</strong>
</span>
#endif
And works great.I want to thank to user:
utdev
For this code:
You need to add / change following in your Controller function:
$request->session()->flash('success', 'Success!');
return redirect()->route("yourstuff.index");
In your blade.php you return that session with it message like this
#if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
#endif
#if (session('error'))
<div class="alert alert-success">
{{ session('error') }}
</div>
#endif
I will use the code proposed by you in other part of my project.
THANKS !

Related

How to show messages on Laravel 9?

Application doesn't show messages as it should.
I tried returning back with message in RoleController. Redirecting works fine, but I can't see any messages. There is multiple functions like this, none of them show messages.
public function store(Request $request)
{
$validated = $request->validate(['name' => ['required', 'min:3']]);
Role::create($validated);
return back()->with('message', 'Role created successfully.');
}
And this code below is in admin.layout blade.
#if (Session::has('message'))
<div class="alert alert-warning alert-dismissible fade show" role="alert">
{{ Session::get('message') }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
#endif
I googled this many many times but can't find a solution. This worked fine month ago, but when I continued with this yesterday, it suddenly stopped working. Php version hasn't changed, it's 8.0.2. I also tried flash-messages with no help.
Sorry, not a native english speaker.
edit / I have also cleared cache with php artisan.
try this
In Controller
return redirect()->route('admin.listing')->with('message', 'Role created successfully.');
In Blade file
#if (Session::has('message'))
<div class="alert alert-warning alert-dismissible fade show" role="alert">
{{ Session::get('message') }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
#endif
I think you can try this...
#if ($message = Session::get('message'))
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
<strong>{{ $message }}</strong>
</div>
#endif
Please try one time with this one
return redirect()->back()->with('message', 'Role created successfully.');
Now it's working. I needed to modify Kernel.php and remove \Illuminate\Session\Middleware\StartSession::class, under the protected $middlewareGroups.

Redirection after sending mail not working | Laravel

What I want:
I want to display a message when the user is redirected back to the form page after a mail is sent.
What I am doing
Here is the code in my SendEmailController.php:
return back()->with('success', 'Mail Sent !');
Here is my code in my view/index.blade.php:
#if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
#endif
I want to mention that I just followed this tutorial: https://www.webslesson.info/2018/09/simple-way-to-sending-an-email-in-laravel.html
In practice :
Nothing is displayed, the main page remain the same and my mail is sent.
What i tried :
I tried setting a route as following:
In my web.php:
Route::get('/succeed', 'PagesController#succeed')
In my PagesController.php:
return view('index')->with('success', 'Mail Sent !');
When I go to http://localhost/succeed, my div is displayed.
In the end, I don't understand why I don't understand why that div don't want to show up.
Edit:
Tried:
redirect()->back()->with('success', 'Mail Sent !');
Not fixing the issue
My problem is fixed, I had some AJAX in the background that was blocking the redirection.
Thanks you a lot for giving me answers.
Like said above, use redirect()->back()->with('success', 'Mail Sent !');
then
use session('success')
#if (session('success'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
#endif

flash message if user is not login laravel

When user tried to go user dashboard without login, it return to login page. it works perfectly. but I need to show message on login page using middleware that 'please login to see this page.'
I tried {!! $errors->first('loginpermission', ':message') !!}
But it not working
Please help me how to use this.
Add these codes in your login.blade page
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
#if (session('error'))
<div class="alert alert-danger">
{{ session('error') }}
</div>
#endif
Add below code in your middleware at condition when user is not logged in
return redirect('/login')->with('error', 'please login to your account');
#if ($errors->has('loginpermission'))
{{ $errors->first('loginpermission') }}
#endif

Laravel : Error messaging not working

Hello friends I have a form when i enter correct value and pressing submit button i am getting success message
if i am entering wrong value then
i am not getting error message please suugest something
Controller
if($getHours <= 16)
{
DB::table('labors')->insert($labors_data);
session::flash('status', 'Labor Timesheet Added Successfully');
return Redirect:: to('ViewLaborD2S');
}
else
{
session::flash('status', 'You have entered More than 16 Hours');
return Redirect:: to('ViewLaborD2S');
}
View
#if (session('status'))
<div class="alert alert-success">
<p style="text-align:center;">{{ session('status') }}</p>
</div>
#else(session('message'))
<div class="alert alert-danger">
<p style="text-align:center;">{{ session('message') }}</p>
</div>
#endif
I want to show Success message if i entered correct value
and if i enter wrong value show error message
Try this:
#if (Session::has('status'))
<div class="alert alert-success">
<p style="text-align:center;">{{ Session::pull('status') }}</p>
</div>
#endif

Laravel 5.1: with() helper is not working

I am using with() helper to pass some error messages to views. My code for this is
redirect('somewhere')->with('message', 'show some message')
Then in the targeted view to catch the message I have this:
#if(count($errors)>0)
<div class="alert alert-success">
<ul >
#foreach($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
</div>
#endif
But no message is delivered to the view. What's the problem here?
redirect('somewhere')->withErrors(['message', 'show some message'])
Please check this link Redirecting With Flashed Session Data
In targeted view you can handle message like this
#if (session('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
#endif

Categories