Symfony / Constrains & Validators on forms (email constrain)

163 Views Asked by At

It's possible to create a custom email constrain validator (like @domain.com) using the "Custom Validation Constraint" of Symfony (http://symfony.com/doc/current/validation/custom_constraint.html) but not using the constrain in the Entity class?

I'm have an multitenant site in symfony and differents formsTypes for each client. One client needs that only can sing up X emails domains. In this case, I can't use a constrain in entity class like this:

class MyEntity
{
    /**
     * @Assert\Email()
     * @CustomAssert\EmailDomain(domains = {"yahoo.com", "gmail.com"})
     */
    protected $email;

Any different idea?

1

There are 1 best solutions below

1
On

Yes, custom annotation can work on any class, they will be used when you call the $validator->validate($yourClassInstantiated)

If you want dynamic data (like domains depending on your client), you can use doctrine inside your ConstraintValidator class.