I have form with three element - name, login and email. In validation.yml
I have:
Mark\UserBundle\Entity\User:
properties:
name:
- NotBlank: ~
login:
- NotBlank: ~
email:
- Email: ~
- NotBlank: ~
It works, this makes name
and login
required. Instead, I need name
or login
required. email
remains always required.
In Symfony 1.4 I can use sfValidatorOr. How can I make it in Symfony 2?
I think you should solve this by using the Callback
constraint instead:
// ...
use Symfony\Component\Validator\ExecutionContextInterface;
class User
{
// ...
public function hasCorrectCreditials(ExecutionContextInterface $context)
{
$isBlank = function ($val) {
return null === $this->name || '' === $this->name;
};
if ($isBlank($this->name)) {
if ($isBlank($this->login)) {
$context->addViolationAdd('login', 'Either name or login should not be blank');
}
}
}
}
Mark\UserBundle\Entity\User:
constraints:
- Callback: { methods: [hasCorrectCreditials] }
properties:
email:
- Email: ~
- NotBlank: ~