This article showcases some minor but useful new features added to Symfony 5.1 that will make your development work easier. This is also the last article in the New in Symfony 5.1 series, which contains 47 articles in total.
Allow to include the severity in ConstraintViolationList
Symfony Validator component allows to include a payload in constraint
violations with the error severity and other data. In Symfony 5.1 we've improved
the Serializer component to also allow adding a payload with data in the
ConstraintViolationList
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
$constraint = new NotNull();
$constraint->payload = ['severity' => 'warning', 'someOtherField' => 'someValue'];
$list = new ConstraintViolationList([
new ConstraintViolation(/* ... */, $constraint),
]);
$result = $this->normalizer->normalize($list, null, [
// pass an array of field names to add only those to the payload
// pass NULL or TRUE to pass all fields
ConstraintViolationListNormalizer::PAYLOAD_FIELDS => ['severity']]
);
$payload = $result['violations'][0]['payload'];
// $payload = ['severity' => 'warning'];
Mailer Log Handler
In your Symfony applications, logging should be managed using dedicated tools or services. However, in some circumstances (e.g. while prototyping, when developing tiny apps, etc.) sending logs via email when an error happens may be a valid solution, all things considered.
That's why in Symfony 5.1 we added a new Monolog log handler which uses the Mailer component to send the logs via email.
New Deprecation Contract
Symfony Contracts are a set of abstractions extracted out of the Symfony components. In Symfony 5.1 we've started using a new contract related to deprecations. The only method provided by the contract is:
1
trigger_deprecation(string $package, string $version, string $message, ...$args): void;
This will provide more meaningful and standardized deprecation messages in your applications, so we've updated Symfony's own code to use the new method:
1 2 3 4 5 6 7 8 9 10 11 12 13
// Before
@trigger_error(
'Not setting the "framework.router.utf8" configuration option is deprecated
since Symfony 5.1, it will default to "true" in Symfony 6.0.',
E_USER_DEPRECATED
);
// After
trigger_deprecation(
'symfony/framework-bundle', '5.1',
'Not setting the "framework.router.utf8" configuration option is deprecated,
it will default to "true" in version 6.0.'
);
Dark Mode in Exception Pages
Adding support for "dark mode" or "dark themes" is increasingly popular in developer tools. We already implement that feature in symfony.com and the Symfony Profiler, but in Symfony 5.1 we also added support for dark mode in exception pages.
Check out the Pull Request #34924 to see some screenshots of this new design.
Thank you for the articles Javier. :)
LOVE the dark mode exceptions :)
Dark mode exceptions 💞