Fetching errors on a page with multiple forms in Laravel - laravel

I am using Laravel 5.2 (although solutions for later versions are also okay).
I have a page, which contains BOTH the login page and registration page.
These forms use the AuthController as usual.
I display the errors like so:
#if (count($errors) > 0)
<div class="callout alert">
<strong>Whoops! Something went wrong!</strong>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
The issue is, the $errors variable does not make it clear which form the errors come from (i.e. Is it errors in the registration form or login form?).
How can I do this?

A way to handle this is to return flash messages. In your controller you can use something like:
For the login form
public function postLogin() {
// your code here
return redirect('/login')->with('login', 'Enter valid details');
}
For the sign up form
public function signUp() {
// your code here
return redirect('/login')->with('signup', 'SignUp has been successful');
}
And in order to display them in the view:
<div class="clearfix">
#if(Session::has('login'))
<div class="toast">
{{ Session::get('login') }}
</div>
#endif
</div>
<div class="clearfix">
#if(Session::has('signup'))
<div class="toast">
{{ Session::get('signup') }}
</div>
#endif
</div>

Related

Target class [App\Http\Controller\ContactController] does not exist. Laravel 9

i am beginner of laravel. i ran into the problem .Target class [App\Http\Controller\ContactController] does not exist.
i set the all the routes files currectly.i don't why this errors is displaying.
ContactController
public function index()
{
$contacts = Contact::all();
return view('contact.index')->with('contacts',$contacts);
}
View Page
#extends('layout')
#section('content')
<div class="card">
<div class="card-header">About</div>
<div class="card-body">
#foreach($contacts as $contact)
<p>{{ $contact->name }}</p>
<p>{{ $contact->address }}</p>
<p>{{ $contact->mobile }}</p>
#endforeach
</div>
</div>
#stop
i attached the screen shot image below.
Routes
use App\Http\Controller\ContactController;
Route::resource('/contact',ContactController::class);
The controllers live in App\Http\Controllers, so edit
use App\Http\Controller\ContactController;
to
use App\Http\Controllers\ContactController;
make sure the file namespace is correct too

Laravel Session Flash Message not showing on live server but shows on localhost

When I flash one particular message it shows on localhost but not on production server. What can be cause of this problem.All other messages are shown but not this one
//Blade Template
#if (count($errors) > 0)
<div class="alert alert-danger">
×
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#foreach (['danger', 'warning', 'success', 'info'] as $msg)
#if(session('alert-' . $msg))
<div class="alert alert-{{ $msg }}">
{{ session('alert-' . $msg) }} ×
</div>
#endif
#endforeach
//Controller Code
return redirect('url')->with('alert-danger','All lines are already reserved, Please try again later.');
``
Also it does show up when I dd() the redirect object, see attached image
[1]: https://i.stack.imgur.com/C9ZXZ.png
Try flashing manually, because redirect return a 301 HTTP response, not a view
/*request()->*/session()->flash('alert-danger', 'All lines are already reserved, Please try again later.');
return redirect('url');

laravel Form Validation errors

version: laravel 5.7
Router:
Route::get('regist','User\RegistController#registView');
Route::post('regist','User\RegistController#regist');
Form:
<form class="form-signin" method="POST" action="/regist">
.....
</form>
Validate:
$this->validator=Validator::make($input,$rule,$message);
if($this->validator->fails()){
return \Redirect::back()->withErrors($this->err());
}
The Problem:
Do not display an error message.Need to press Enter in the address bar to reload the page.Want to use the Validateor::make method.How can I modify it?
If the validation worked, did your put the error message to be displayed on the page you redirected to?
for example in your blade page did you do something like below?
#if(count($errors->all()) > 0)
<div class="alert alert-danger" role="alert">
<p><b>Required Fields Missing!</b></p>
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
I will show you a simple example, if you are in the controller use the validator facade
use Illuminate\Support\Facades\Validator;
In the conroller user the below code
public function update(Request $request, $id) //use of request
{
$this->validate($request, [
'name' => 'required',
], [
'name.required' => 'Name is required',
]);
}
If validation fails it will automatically redirect back or you could use a validator fail statement and send a modified message.
After that you should put an if statement to display the error message as below:
#if(count($errors->all()) > 0)
<div class="alert alert-danger" role="alert">
<p><b>Required Fields Missing!</b></p>
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Or if you have only one defined error message use:
#if(Session::has('success'))
<div class="alert alert-success" role="alert">
{ Session::get('success') }}
</div>
#endif
Hope this helps
For add validation on form you can use Laravel Request Validation classes. which will help you to pull validation in separate class.
Here is the reference for request classes :- https://laravel.com/docs/5.7/validation#form-request-validation
That will help you to pull validation and also improvements in your code.
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif

Error partial not showing messages

The following code is in a partial named errors.blade.php:
#if ($errors->any())
<div class="form-group">
<div class="alert alert-danger">
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
</div>
#endif
It's being called in master.blade.php:
<body>
#include("layouts.nav")
<div class="container-fluid">
<div class="row">
#include("layouts.sidebar")
#yield("content")
#include("layouts.errors")
</div>
</div>
#include("layouts.footer")
</body>
After submitting a form and using the validate() function, nothing is shown on the page that a user is redirected to. If I take the code from errors.blade.php and put it directly into the products.index.blade.php file, the errors show as intended. This is not desirable as I want errors and partials of a similar nature to be shown across all views if applicable.
Why aren't they working?
I see what mean and I do the same but instead of in master I only include it in the page that need it like form pages, login and registration.

Laravel Flash errors - How to check if a checkbox is checked?

I'm trying to do flash messages in Laravel, now the flash messages work for success messages and error messages on pages that don't have checkboxes.
I have a view called 'deleteappointmentform' which requires the user to check a checkbox and it deletes the checked appointments, however if I don't check any checkbox and click submit it gives me a success message without actually checking if they've checked and checkboxes. I'm trying to get it to display an error message if they don't check any checkboxes
Any help's appreciated, thanks
This is the function that deals with deleting appointments
function deleteAppointment(Request $request)
{
Appointment::destroy($request->appointments);
Session::flash('successCancelAppointment', 'Appointment cancelled successfully!');
return redirect('all');
}
This is my messages blade
#if (Session::has('successCancelAppointment'))
<div class="alert alert-success" role="alert">
<strong>Success: </strong> {{Session::get('successCancelAppointment')}}
</div>
#endif
#if (count($errors) > 0)
<div class="alert alert-danger" role="alert">
<strong>Errors:</strong>
<ul>
#foreach ($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
</div>
#endif
This is my deleteappointmentsblade
#extends('layouts.master')
#section('title', 'Cancel Appointment')
#section('content')
<form action="{{url('deleteappointment')}}" method="POST">
{{ csrf_field() }}
#foreach ($appointments as $appointment)
<div>
<label> {{$appointment->user->firstname}} {{$appointment->user->surname}}</label>
<label>Has an appointment at: {{$appointment->time}}</label>
<label>On: {{$appointment->date}}</label>
<label>With Dr: {{$appointment->doctor->surname}}</label>
<input type='checkbox' value='{{$appointment->id}}' name='appointments[]'/>
</div>
#endforeach
<input type="submit" name="submitBtn" value="Cancel Appointments">
</form>
#endsection
you can try this
function deleteAppointment(Request $request)
{ $rules=array(
'appointments'=>'required'
);
$validator = Validator::make($request->all(), $rules);
if($validator->fails())
{
$messages = $validator->messages();
$errors = $messages->all();
return redirect()->back()->withErrors($errors);
}
Appointment::destroy($request->appointments);
Session::flash('successCancelAppointment', 'Appointment cancelled
successfully!');
return redirect('all');
}

Resources