Laravel 4 Login Credentials mismatch does not send back error data - validation

Everything is fine, Login, Validation, and Username/Password combination mismatch, except for the mismatch, it does not send back the data that I'm sending.
This is my code, and I've tried different login techniques, and same issue.
This is my blade page errors section:
<div id="formErrorDiv">
#if ($errors->has())
<h2>* Please check your errors.</h2>
#endif
#if (isset($mismatch))
<h2>* {{ $mismatch }}</h2>
#endif
</div>
while the $errors-has() for the fields' validation works perfectly, the $mismatch variable always turns out empty.
This is my controller login section:
if($auth){
return Redirect::intended('/');
} else {
//dd('auth else');
$mismatch = 'Login Credentials Error.';
return Redirect::route('account-sign-in')
->with('mismatch', $mismatch);
}
although when I use the dd('auth else') it does work, and so it enters the else section but always sends nothing with the redirection.

You need to use Session::get() to retrieve the value you pass with with():
#if(Session::has('mismatch'))
<h2>* {{ Session::get('mismatch') }}</h2>
#endif
errors is the exception here. Because it's very common to pass that to the next view, Laravel built it in so the errors variable from the session is automatically available in your views...

Related

Laravel 7: How to clear withErrors session

I'm using Gate for permissions and redirect the user to the home if he doesn't have enough permission with an error message
if(Gate::denies('manage-users')){
return redirect(route('home'))->withErrors('You don\'t have enough permissions!');
}
But when the user navigates to another route with correct permission the page displays correctly but with an error handler in the view saying the same message "you don't have enough permissions"
How can I clear errors session once the error get displayed in home to hide it from other views?
Don't know if this is the best solution so please correct me. I Changed the validation to be this
if(Gate::denies('manage-users')){
return redirect(route('home'))->withErrors(['permission_error' => 'You don\'t have enough permissions!']);
}
In the home view
#if(session()->has('permission_error'))
{{session('errors')->get('permission_error')}}
#php session()->forget('permission_error') #endphp
#elseif( !session()->has('permission_error') && $errors->any())
<div class="alert alert-danger">
{{ $errors->first() }}
</div>
#endif

Laravel: How to log errors in form request input

I am using Laravel's form request to validate some complex input. If there is an error, Laravel returns the user to the previous page (which is fine), but I also want it to log the errors for me. How do I do that?
I attempted to use the form's withValidator method as follows, but got an error re 'maximum nesting level reached'.
public function withValidator($validator)
{
$validator->after(function ($validator) {
if ($validator->fails()) {
foreach($validator->errors()->all() as $error) {
logger($error);
}
}
});
}
Calling $validator->fails() in the after callback will end in an endless loop as it calls $validator->after() in the process. It re-evaluates and does not use a previous result.
Use $validator->errors() instead and check if it is empty.
Best way to validate on laravel is to use Laravel Request
As mentioned in the Laravel Documentation,
If the incoming request parameters do not pass the given validation rules, Laravel will automatically redirect the user back to their previous location.
In addition, all of the validation errors will be present in the $error variable.
The $errors variable is bound to the view by the Illuminate\View\Middleware\ShareErrorsFromSession middleware, which is provided by the web middleware group. When this middleware is applied an $errors variable will always be available in your views, allowing you to conveniently assume the $errors variable is always defined and can be safely used.
To view the errors, you can add the following snippet inside your view which is returned when the validation is failed:
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif

How to use return Redirect, inside View?

i have view page
#if((Voyager::can('delete_'.$dataType->name))||(Voyager::can('edit_'.$dataType->name))||(Voyager::can('browse_'.$dataType->name)))
some function here
#elseif
return Redirect::to('404page')
#endif
error
ErrorException in VerifyCsrfToken.php line 156: Trying to get property
of non-object
else condition code is not working.How can i return inside from one to another.any help would be appreciated.
You can't use a redirect response in a view. A redirect response is a response with code 3XX. Response codes are part of the header, but you can only send the headers once. To send view data you need to have already sent the headers therefore you can no longer return a redirect response. You can however use JavaScript:
#if((Voyager::can('delete_'.$dataType->name))||(Voyager::can('edit_'.$dataType->name))||(Voyager::can('browse_'.$dataType->name)))
some function here
#else
<script> window.location.href = "{!! url()->to('404page') !!}"; </script>
#endif
Note that this will actually load the page and the user might prevent the redirect from happening so don't show anything restricted.
Ideally you'd need to return the redirect response before the view in the controller or middleware
Change #elseif to #else
#if((Voyager::can('delete_'.$dataType->name))||(Voyager::can('edit_'.$dataType->name))||(Voyager::can('browse_'.$dataType->name)))
//
#else
Redirect::to('404page')
#endif
And user redirect() instead of Redirect::to. Or if you want to send a user to 404 error page, use abort('404').

Redirect with success or error message without using sessions

How can i redirect with success or error message without using sessions in laravel
Currently I am using the following code for redirect :
return redirect('dashboard')->with('status', 'Profile updated!');
But this code Need session to display the success message.
You can set another variable status type along with the status like below,
return redirect('dashboard')->with(['status'=>'Profile updated!','status_type'=>'success']);
And in your dashboard blade file use
#if(isset($status))
<p class="alert alert-{{ $status_type }}" >{{ $status }}</p>
#endif

Laravel 4 - Password reminder errors not showing

Im following the Laravel 4 docs in regards to the password reset functions.
The first bit seems to be working, except for the fact that im not seeing any error or success messages in my view.
I have used the controller that comes with laravel .. This is what i have in the controller.
switch ($response = Password::remind(Input::only('email')))
{
case Password::INVALID_USER:
return Redirect::back()->with('error', Lang::get($response));
case Password::REMINDER_SENT:
return Redirect::back()->with('status', Lang::get($response));
}
and the following is what i have in my view.
#if (Session::has('error'))
{{ trans(Session::get('reason')) }}
#elseif (Session::has('success'))
An email with the password reset has been sent.
#endif
Also, can anyone tell me how to see what validation rules the password reminder has.
change
return Redirect::back()->with('status', Lang::get($response));
to
return Redirect::back()->with('success', Lang::get($response));
and change
{{ trans(Session::get('reason')) }}
to
{{ trans(Session::get('error')) }}

Resources