Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"errors" using middleware overwrites laravel $errors variable #95

Closed
hoRacy opened this issue Aug 5, 2020 · 2 comments
Closed

"errors" using middleware overwrites laravel $errors variable #95

hoRacy opened this issue Aug 5, 2020 · 2 comments

Comments

@hoRacy
Copy link

hoRacy commented Aug 5, 2020

  • Package Version: newest
  • Laravel Version: all (7.x used)

Description:

Hello.
When i use for example
return back()->with('errors', 'Error messagge');
and simultaneously use $errrors variable in form on blade templates like on official documentation
https://laravel.com/docs/7.x/validation#quick-displaying-the-validation-errors

@if ($errors->has('postcode')) <span class="invalid-feedback"> <strong>{{ $errors->first('postcode') }}</strong> </span> @endif

i've got the error "call to a member function has() on string", because library "errors" overwrites laravels $errors

Possible resolution is to add prexif to sweet alert "errors" variable, like in "toast_error", for example
return back()->with('modal_error', 'Error messagge');
return back()->with('modal_success', 'success messagge');

@realrashid
Copy link
Owner

Hey @hoRacy ,

Actually I didn't get your point because if you want SweetAlert catch your errors so you don't need to do anything except the enabling the middleware option through the .env file.

Register the middleware in web middleware groups by simply adding the middleware class

\RealRashid\SweetAlert\ToSweetAlert::class,

into the $middlewareGroups of your app/Http/Kernel.php file.

Set the SWEET_ALERT_AUTO_DISPLAY_ERROR_MESSAGES .env value to true to activate the automatic displaying for the validation error messages.
By default, this is not activated.

SWEET_ALERT_AUTO_DISPLAY_ERROR_MESSAGES=true
request()->validate([
        'first_name' => 'required',
        'last_name' => 'required',
        'email' => 'required'
]);
$contact = new Contact([
    'first_name' => $request->get('first_name'),
    'last_name' => $request->get('last_name'),
    'email' => $request->get('email'),
    'job_title' => $request->get('job_title'),
    'city' => $request->get('city'),
    'country' => $request->get('country')
]);
$contact->save();
return redirect('/contacts')->with('toast_success', 'Contact saved!');

Now any validation errors will catch by SweetAlert you don't need to do anything.

@realrashid
Copy link
Owner

Closed due to inactivity !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants