framework icon indicating copy to clipboard operation
framework copied to clipboard

[Proposal] Add Redirect::to()->withSuccess() - opposite to withErrors()

Open DanSmith70 opened this issue 13 years ago • 27 comments

Hi All,

I was wondering if there was any interest in adding the ability to use withSuccess when redirecting. Support for this would also need to be added to ViewServiceProvider.php to make the success messages global over all views. This would let you build in to your views a global (or per view) ability to respond to a success message much as we can do with error messages right now.

Some example use cases:

Redirect::home()->withSuccess('Thanks for registering, please check your email for an activation link');
Redirect::home()->withSuccess('Activation complete');
Redirect::home()->withSuccess('Your password reset email has been sent');
Redirect::home()->withSuccess('Your password has been updated');

Then perhaps in your master layout file you could add (blade/bootstrap example):

@if($success->any())
    <div class="alert alert-success alert-block">
        <button type="button" class="close" data-dismiss="alert">&times;</button>
        <h4>Success</h4>
        <ul>
            {{ implode('', $success->all('<li>:message</li>')) }}
        </ul>
    </div>
@endif

That would be optional of course, you could deal with it on a per view basis or not at all if you wanted.

Obviously this is possible with session flashing and then checking for the existence of the variable right now, but as we assured to always have an $errors MessageBag (empty or not) it makes sense to me to also have $success MessageBag too. This also reduces boilerplate on what must be a fairly common operation for a lot of users.

Thoughts?

Edit: I dont mind doing the PR for this if there is interest.

DanSmith70 avatar Apr 12 '13 14:04 DanSmith70

+1, or rename the $errors to $messages

crissi avatar Apr 12 '13 18:04 crissi

Why not @if($errors->none()) ? I don't think it makes sense the way you present it, if there is any error you shouldn't have a success message.

Anahkiasen avatar Apr 12 '13 18:04 Anahkiasen

@Anahkiasen One point against that is if you are using say Redirect::back(). $errors->none() will be true even if they haven't done anything.

In my mind "success" isn't just a lack of errors.

WMeldon avatar Apr 12 '13 18:04 WMeldon

I think the problem here is trying to implement a notion of success on an $error variable. Whatever you do it won't semantically be correct I think. I'm more in favor of the $success variable mentionned in the first post and then  $success->any() or something.

Anahkiasen avatar Apr 12 '13 18:04 Anahkiasen

Differentiating between success and errors in such a way could have some hidden complexity, but I still agree that it's a better solution.

I really don't think it's a good idea for successes to be found in errors.

WMeldon avatar Apr 12 '13 18:04 WMeldon

I think the example @Bulk70 have has a typo, it should be $success->any() and not $errors->any()

brunogaspar avatar Apr 12 '13 19:04 brunogaspar

That makes a lot more sense

WMeldon avatar Apr 12 '13 19:04 WMeldon

Sorry yes @brunogaspar is right that was a copy/paste mistake, fixed now!

DanSmith70 avatar Apr 12 '13 22:04 DanSmith70

Is it worthwhile just making this as flexible as possible?

->with('errors', 'This is an error message') // can be aliased to ->withErrors()
->with('success', 'It works!');
->with('foo', 'This is a foo message');

All this is really doing under the hood is using flash messages, so its pretty easy to do this manually anyway.

JonoB avatar Apr 14 '13 14:04 JonoB

Redirect::home()->with(['success' => 'message here']); is what I do and I think it is perfectly fine.

However I use have this stuff handled with a base class. If you do something often then think like a programmer and make it so you do it less often instead of requesting a specific method. Mine is $this->toAction('index', ['success' => 'message']);

robclancy avatar Apr 14 '13 15:04 robclancy

Its not quite just a flash message, it passes the errors though using a MessageBag which has built in formatting functionality amongst other benefits. My plan was to genericize the internal functions somewhat anyway as otherwise there would be a bunch of repeated code.

The other side of the problem is presenting the data to the views - which is the real point of this. The nice thing about $errors is you know its always there, and always going to be a MessageBag so you don't ever need to do an isset() on it, you can just use $errors->any() to check. To keep that same level of functionality with a generic message object is actually not so simple, because MessageBag's are not nested or associative in any way.

I do like the idea of passing a single generic messages variable to all views, which could then contain an array or object of some kind which could be parsed further. However I'd like to avoid the need have an isset on an array index or something like that. I need to have a look at the Collection object code and see if it could be used for associative data, but I think its probably more geared towards non-associative arrays of data right now - another proposal in it's self.

DanSmith70 avatar Apr 14 '13 23:04 DanSmith70

Well what I basically do now, which is probably not the most "correct" way, is just flash it and do if ($success = Session::get('success'). Same thing for other alerts. Usually people have a helper.alerts view which will handle all these things.

I'm not against this idea I just don't know if it is something that should be in the core.

robclancy avatar Apr 15 '13 01:04 robclancy

I like how the errors can be effortlessly passed back to the views. I agree that a positive message would be handy to have similar functionality to the negative (such as errors). Perhaps success is a bit too descriptive or rigid. Maybe we could call it feedback. Making the method withFeedback().

helmut avatar Apr 15 '13 03:04 helmut

@robclancy Yeah thats the only way right now, personally I just dont like that $errors is a nicley defined messagebag and we have to fend for our selves for the rest. Clearly it's not complicated code to handle it your self, it just feels "missing" to me.

To be honest I dont want to spend the time making and writing tests for this if @taylorotwell isn't onboard with it.

DanSmith70 avatar Apr 15 '13 07:04 DanSmith70

+1 to this, would be nice to have the opposite shorthand

return \Redirect::to('/')
        ->with('successes', $bag);
return \Redirect::to('/')
        ->withSuccesses($bag);

spamoom avatar Apr 15 '13 20:04 spamoom

in my validation. i set the flush message

Session::flash('successMsg', 'Account successfully saved.');

in my BaseController, i set the success message as global to all my views, such like this

protected function setupLayout()
{

    View::share(array(
        'auth' => Auth::user(),
        'successMsg' =>  Session::get('successMsg'),
    ));

}

then, call the successMsg on a certain view

@if($successMsg)

{{$successMsg}}

@endif

jurerick avatar Jul 20 '13 17:07 jurerick

I don't think this is really necessary at the moment.

taylorotwell avatar Jul 29 '13 18:07 taylorotwell

There is withErrors() method, I think it should have full functionality, if you are not going to implement methods like withSucces(), withNotice(), withInfo(), withWarning(), withPopup(), it's better to remove withError() then. Make it consistent. IMHO.

vedmant avatar Oct 04 '15 01:10 vedmant

It makes absolutely no sense to remove a perfectly good method just because another method doesn’t exist.

On Sat, Oct 3, 2015 at 8:40 PM, Vedmant [email protected] wrote:

There is withErrors() method, I think it should have full functionality, if you are not going to implement methods like withSucces(), withNotice(), withInfo(), withWarning(), withPopup(), it's better to remove withError() then. Make it consistent. IMHO.

Reply to this email directly or view it on GitHub: https://github.com/laravel/framework/issues/906#issuecomment-145305713

taylorotwell avatar Oct 04 '15 03:10 taylorotwell

Wow, didn't expect you to answer me! I'm just talking that having withErrors() makes a lot of people think that something like withSuccess() exists, but it doesn't. This is just how people think, they suppose if there is "A", then it have to be and "B" as well, but in this case there is only "A" and it makes filling that something is not complete here. If there were no method withErrors(), people wouldn't think that there may be something like withSuccess().

vedmant avatar Oct 04 '15 03:10 vedmant