2017-08-31 52 views
0

我在我的項目代碼:Symfony的2種形式 - 比較兩個領域

->add('email', 'text', array(
    'translation_domain' => 'KlientRejestracja', 
    'label' => 'E-mail (login):', 
    'constraints' => array(
     new NotBlank(array('message' => 'emailisNotValid')), 
     new Email(array('message' => 'emailisNotValid')), 
    ) 
)) 
->add('nick', 'text', array(
    'label' => 'Podaj pseudonim<span style="color: red">*</span>:', 
    'translation_domain' => 'KlientRejestracja', 
    'required' => false, 
    'empty_data' => '', 
    'constraints' => array(
     new NotEqualTo(array('value' => '??????','message' => 'imieisNotValid')) 
    )    
)) 

我如何比較字段「電子郵件」和「缺口」,以檢查是否是不一樣的? 我試圖使用NotEqualTo但我不知道應該怎樣比較值。

+0

你使用這種形式與數據類? –

+0

是的,我使用數據類 –

回答

0

添加回調驗證數據類

use Symfony\Component\Validator\Constraints as Assert; 
use Symfony\Component\Validator\Context\ExecutionContextInterface; 

class xxx 
{ 
... 
    /** 
    * @Assert\Callback() 
    */ 
    public function validate(ExecutionContextInterface $context, $payload) 
    { 
     if ($this->email == $this->nick) { 
      $context->buildViolation('error text') 
       ->atPath('email') 
       ->addViolation(); 
     } 
    } 
... 
} 

https://symfony.com/doc/current/reference/constraints/Callback.html