Discuss your project
Web architecture

Symfony 8.2: test validation that depends on a user role

A note can have a different length limit depending on the role. Try Symfony 8.2 validation on the web and check what happens when an import processes the same data.

Symfony 8.2 : tester une validation qui dépend du rôle de l’utilisateur

You limit a contributor’s note to 200 characters. An editor can write 500. The same object arrives in an overnight import that needs its own validation rule.

From form to background job

On October 28, Symfony showed how functions like is_granted() can be used in constraints such as When and Expression with Validator and Security installed. These validation expressions require a request. Evaluation in console or a Messenger worker raises an exception. As of November 20, the release page lists 8.2.0-BETA2 and PHP 8.4 or higher.

What to check in your application

In a test project, submit a 240-character note under each role. Expect a rejection for the contributor and acceptance for the editor. Then run validation outside a request. Give the import an explicit rule independent of the session and test it at both length limits.

The constraint validates the value. The application must also authorize the operation itself. Keep separate assertions for both in your tests.

Share this article