Skip to content

Flash message behavior changed #28991

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

Closed
XWB opened this issue Oct 26, 2018 · 6 comments · Fixed by #36913
Closed

Flash message behavior changed #28991

XWB opened this issue Oct 26, 2018 · 6 comments · Fixed by #36913

Comments

@XWB
Copy link
Contributor

XWB commented Oct 26, 2018

Symfony version(s) affected: 4.1.3

Description

When saving a boolean to the flash bag, the value is converted to an integer.

The code example below has worked for years, and is currently broken. Has something changed in Symfony?

How to reproduce

if ($form->isSubmitted() && $form->isValid()) {
    $this->addFlash('status', true);

    return $this->redirectToRoute(...);
}

$this->addFlash('status', false);
{% from 'general/notify.html.twig' import error, success %}

{% for status in app.flashes('status') %}
    {% if status is same as(true) %}
        {% success('Saved successfully' | trans) %}
    {% elseif status is same as(false) %}
        {% error('An error occurred' | trans) %}
    {% endif %}
{% endfor %}

The message will not be printed. After some digging, it turns out that boolean values are converted to integers.

image

Note: our session data is saved in Memcached.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Oct 28, 2018

Flash messages have always been documented as being strings. Anything else worked by chance I suppose. Now that we can use type hints, this is enforced. You should update your code here.

@OskarStark
Copy link
Contributor

Maybe caused by some declare strict types?

@xabbuh
Copy link
Member

xabbuh commented Oct 30, 2018

@XWB Do you observe the same behaviour when using another storage like the filesystem?

@maryo
Copy link
Contributor

maryo commented Nov 7, 2018

I know it has been documented as being a string for years but I was always "misusing" the missing typehint 😈 by passing a value object like this

class FlashMessage
{
    const TYPE_SUCCESS = 'success';
    const TYPE_INFO = 'info';
    const TYPE_WARNING = 'warning';
    const TYPE_DANGER = 'danger';

    /** @var string */
    private $message;

    /** @var array */
    private $parameters;

    /** @var string|null */
    private $domain;

    /** @var string|null */
    private $locale;

    public function __construct(string $message, array $parameters = [], ?string $domain = null, ?string $locale = null)
    {
        $this->message = $message;
        $this->parameters = $parameters;
        $this->domain = $domain;
        $this->locale = $locale;
    }

    public function message(): string
    {
        return $this->message;
    }

    public function parameters(): array
    {
        return $this->parameters;
    }

    public function domain(): ?string
    {
        return $this->domain;
    }

    public function locale(): ?string
    {
        return $this->locale;
    }

    public function __toString(): string
    {
        return $this->message;
    }
}
{% include 'alert.html.twig' with {
    message: message is instance of('FlashMessage')
        ? message.message|trans(message.parameters, message.domain ?? 'flashes', message.locale)
        : message
} %}

In my case the only issue is addFlash method inside
vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php
and actually I'm not forced to use it but I'd like to.

/**
 * Adds a flash message to the current session for type.
 *
 * @throws \LogicException
 *
 * @final
 */
protected function addFlash(string $type, string $message)

I know it's sort of a hack but I still like it. Couldn't the typehint be removed? 👼
Or couldn't the string type constraint be dropped at all?

@nicolas-grekas
Copy link
Member

you can try submitting a PR. I don't promise anything!

@xabbuh
Copy link
Member

xabbuh commented Nov 24, 2018

In fact, the FlashBagInterface accepts any type of message (using mixed as the type explicitly). So this only happens when using the ControllerTrait.

fabpot added a commit that referenced this issue Jun 10, 2020
…addFlash() (ThomasLandauer)

This PR was merged into the 3.4 branch.

Discussion
----------

[FrameworkBundle] fix type annotation on ControllerTrait::addFlash()

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #28991 Fix #34645
| License       | MIT
| Doc PR        | not yet, see below

Removing `string` type-hint of $message at addFlash()

Closes #28991 and #34645

Reasons:

* `addFlash()` is just a convenience shortcut for `FlashBagInterface::add()` which doesn't have the type hint: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php#L28 . So removing it here improves consistency.

* #28991 (comment) is a valid use case for having an object as `$message`.

* Twig doesn't have any rendering helpers for the `message`, see https://symfony.com/doc/current/controller.html#flash-messages . And since users have to take care of displaying the `message` themselves, there's no reason to force a string upon them.

* This isn't a real new feature, but it isn't a bugfix either ;-)
* I didn't update `src/**/CHANGELOG.md` yet.
* I'm not sure if it's necessary to update the docs. Maybe a short note https://symfony.com/doc/current/controller.html#flash-messages ?

Commits
-------

dfb4614 Update AbstractController.php
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this issue Jun 10, 2020
Removing `string` type-hint of $message at addFlash()

Closes symfony/symfony#28991 and symfony/symfony#34645

Reasons:

* `addFlash()` is just a convenience shortcut for `FlashBagInterface::add()` which doesn't have the type hint: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php#L28 . So removing it here improves consistency.

* symfony/symfony#28991 (comment) is a valid use case for having an object as `$message`.

* Twig doesn't have any rendering helpers for the `message`, see https://symfony.com/doc/current/controller.html#flash-messages . And since users have to take care of displaying the `message` themselves, there's no reason to force a string upon them.
@fabpot fabpot closed this as completed Jun 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants