简体   繁体   中英

Symfony2 validation - override checkMX constraint in TEST environment

I have a Symfony form with validation set up (using definitions in validations.yml file). In this form is an email field, to which I have added the checkMX validator constraint. This works fine under normal usage. http://symfony.com/doc/current/reference/constraints/Email.html#checkmx

However, when running my test suite, with no internet connection, this stalls the test as the validator tries to look up the DNS of the email address domain, and it cannot look up the DNS and has to wait for the timeout. In any case requiring a remote lookup during testing is not ideal.

Is there a way to remove this constraint from the validation but only when the TEST environment is run? Presumably I just need to override the constraint but I don't know how to do this per-environment.

Thanks

You can get your environment from the controller and pass it to your form:

$env = $this->container->get( 'kernel' )->getEnvironment();
$oForm = $this->createForm(new Form($env));

Then before your BuildForm:

 public function __construct($env)
{
    $this->sEnv  = $env;
}

And then you do your test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM