0

If I create a custom constraint and constraintvalidator in Symfony2, I have to attach it to my entity using the fully qualified name. I'm aware I can alias that name as a config variable, but I'm wondering if I can add my namespace to the ones Symfony will load from, So I can use MyConstraint rather than FQDN/Path/To/MyConstraint or some.definition.of.myconstraint

Charles
  • 50,943
  • 13
  • 104
  • 142
jhogendorn
  • 5,931
  • 3
  • 26
  • 35

2 Answers2

1

In your validation.yml file you can add following

namespaces
  any_name: Path\To\Your\Validator\Namespace\

Then you can do

FQCN\Of\Entity:
  constraints:
    - "any_name:YourConstraint": ~ 
Mun Mun Das
  • 14,992
  • 2
  • 44
  • 43
  • This is an acceptable solution, and thanks very much for it, but I'm not going to tick it as the answer just yet because it doesn't really meet the original questions intent. Perhaps I can add the namespaces to another config somewhere and not have to use the Prefix:Name notation? – jhogendorn Mar 21 '12 at 04:45
  • AFAIK `namespaces` section are valid for single `validation.yml`. Perhaps you can put the entities that uses the constraint in same `validation.yml` file. – Mun Mun Das Mar 21 '12 at 05:23
  • I am doing that currently, but it would be a better solution to do something akin to what zend does, and allow compilation of namespaces to look through for the requested validator. – jhogendorn Mar 21 '12 at 06:42
  • Just checked it. Well, I was wrong. If you define `namespaces` in different file, you still get the reference of the namespace. You can not reference it without the prefix. But you can create a separate `validation_config.yml` where your namespaces will be listed. See my another answer [here](http://stackoverflow.com/a/9414343/1218997). – Mun Mun Das Mar 21 '12 at 09:05
0

I think you want to do the following in your EntityClass:

use FQDN/Path/To/MyConstraint AS MyConstraint;

Afterwards, all Classes within the MyConstraint namespace can be used within annotation:

@MyConstraint\SomeConstraint()
Sgoettschkes
  • 13,141
  • 5
  • 60
  • 78