2013-12-10 43 views
0

我有三個元素的形式 - 名稱,登錄和電子郵件。在validation.yml我:表單驗證 - 組OR條件

Mark\UserBundle\Entity\User: 
    properties: 
     name: 
      - NotBlank: ~ 
     login: 
      - NotBlank: ~ 
     email: 
      - Email: ~ 
      - NotBlank: ~ 

它的工作原理,這使得name和所需login。相反,我需要nameloginemail始終需要。

在Symfony 1.4中,我可以使用sfValidatorOr。我如何在Symfony 2中創建它?

回答

0

我想你應該使用Callback約束,而不是解決這個問題:

// ... 
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: ~