This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/tests/Symfony/Tests/Component
Fabien Potencier 6b9a355fb0 merged branch blogsh/dynamic_constraints (PR #3114)
Commits
-------

92f820a Renamed registerConstraints to loadDynamicValidatorMetadata
dd12ff8 CS fix, getConstraints renamed
09c1911 [Validator] Improved dynamic constraints
54cb6e4 [Validator] Added dynamic constraints

Discussion
----------

[Validator] Dynamic constraints

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes

By now the Validator component is based on a per-class configuration of
constraints, but in some cases it might be neccessary to add new constraints
dynamically at runtime.
This pull request adds a "ConstraintProviderInterface" to the Validator component. If an object is validated that implements this interface the method "getConstraints" is used to add dynamic constraints:

    class User implements ConstraintProviderInterface
    {
        protected $isPremium;
        protected $paymentInformation;

        public function getConstraints(ClassMetadata $metadata)
        {
            if ($this->isPremium) {
                $metadata->addPropertyConstraint('paymentInformation', new NotBlank());
            }
        }
    }

---------------------------------------------------------------------------

by alexandresalome at 2012-01-15T11:20:04Z

Related to #1151

---------------------------------------------------------------------------

by canni at 2012-01-16T09:22:28Z

👍

---------------------------------------------------------------------------

by bschussek at 2012-01-16T12:32:44Z

I think this is a good addition. I think we still have a naming problem though. When constraints are loaded using a static method, the default name for the loader method is `loadValidatorMetadata`. Since the method for dynamic constraint loading is basically the same, I think the two names should be related.

Solution (1): Rename the method in your interface to `loadDynamicValidatorMetadata`. Ugly and long.

    class MyClass implements ConstraintProviderInterface
    {
        public static loadValidatorMetadata(ClassMetadata $metadata) ...

        public loadDynamicValidatorMetadata(ClassMetadata $metadata) ...
    }

Solution (2): Rename the default method name in `StaticMethodLoader` to `registerConstraints` and adjust the docs. Breaks BC.

    class MyClass implements ConstraintProviderInterface
    {
        public static registerConstraints(ClassMetadata $metadata) ...

        public registerDynamicConstraints(ClassMetadata $metadata) ...
    }

@fabpot: Are we allowed to break BC here? If not, we should probably stick to (1).

---------------------------------------------------------------------------

by fabpot at 2012-01-16T12:36:14Z

I would prefer to not break BC if possible.

---------------------------------------------------------------------------

by blogsh at 2012-01-16T15:25:46Z

So "loadDynamicValidatorMetadata" would be the best solution?

---------------------------------------------------------------------------

by althaus at 2012-01-17T13:39:19Z

>So "loadDynamicValidatorMetadata" would be the best solution?

Sounds fine for me based on @bschussek's comment.
2012-01-22 10:26:39 +01:00
..
BrowserKit [BrowserKit] first attempt at fixing CookieJar (closes #2209) 2011-12-31 16:32:51 +01:00
ClassLoader merged 2.0 2011-09-28 16:08:31 +02:00
Config [Tests] Use proper assertion for counting 2011-12-19 10:00:41 +01:00
Console [Console] Fix tests on windows 2012-01-09 17:05:16 +01:00
CssSelector [DoctrineBridge] fixed some CS 2011-12-13 10:22:12 +01:00
DependencyInjection [CS] Fix usage of assertCount 2012-01-18 14:42:47 +01:00
DomCrawler [CS] Fix usage of assertCount 2012-01-18 14:42:47 +01:00
EventDispatcher [CS] Fix usage of assertCount 2012-01-18 14:42:47 +01:00
Finder [Finder] added a convenience method Finder::create() 2011-06-14 14:18:24 +02:00
Form merged branch canni/fix_cs (PR #3146) 2012-01-22 10:15:34 +01:00
HttpFoundation [CS] Fix usage of assertCount 2012-01-18 14:42:47 +01:00
HttpKernel [CS] Fix usage of assertCount 2012-01-18 14:42:47 +01:00
Locale [Locale][Testing] Fixed breaking tests if 'intl' extension is not installed (#3139) 2012-01-17 18:44:55 +01:00
Process merged 2.0 2011-12-20 20:16:39 +01:00
Routing [Routing] removed trailing slash support for routes that are not available for GET/HEAD methods (as the redirection will always occurs with a GET/HEAD request, closes #2626) 2011-12-30 19:30:23 +01:00
Security [CS] Fix usage of assertCount 2012-01-18 14:42:47 +01:00
Serializer [Serializer] fixed unit tests after previous merge + added a new one 2012-01-22 10:20:46 +01:00
Templating [Templating] fixed 'scheme://' not detected as absolute path 2011-12-08 15:42:21 +01:00
Translation [Translation] Po/MoFileLoader parse plurization rules 2012-01-03 18:44:44 +01:00
Validator merged branch blogsh/dynamic_constraints (PR #3114) 2012-01-22 10:26:39 +01:00
Yaml merged 2.0 2011-12-18 14:48:17 +01:00