Hi Dan,

Could you please tell me your exact use case? The cleanest way would
probably be, as mentioned above, to create is...Valid() methods. If
that's not feasible, maybe you should subclass your entity instead?
(e.g. Post <- PublishedPost)

If none of that is possible, you can use the Execute constraint to
solve your problem:

use Symfony\Component\Validator\ExecutionContext;

/**
 * @validation:Execute("validateEnabled")
 */
class SomeEntity
{
   /**
    * @validation:NotBlank(groups="Enabled");
    */
   protected $foo;

   /**
    * When true we want to validate $foo
    */
   protected $enable_foo;

    public function validateStrictIfEnabled(ExecutionContext $context)
    {
        if ($this->enable_foo) {
            $metadata =
$context->getMetadataFactory()->getClassMetadata(__CLASS__);
            $propertyPath = $context->getPropertyPath();
            $context->getGraphWalker()->walkObject($metadata, $this,
'Enabled', $propertyPath);
        }
    }
}

But, I must emphasize, look for a problem in your architecture before
you use such measures.

Bernhard

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en

Reply via email to