-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Comments
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. |
Maybe caused by some declare strict types? |
@XWB Do you observe the same behaviour when using another storage like the filesystem? |
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 /**
* 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? 👼 |
you can try submitting a PR. I don't promise anything! |
In fact, the |
…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
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.
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
The message will not be printed. After some digging, it turns out that boolean values are converted to integers.
Note: our session data is saved in Memcached.
The text was updated successfully, but these errors were encountered: