2016-11-24 128 views
1

我有來自實體的一些數據的表單類型。Symfony如何從默認驗證組中刪除約束

有一個字段是必需的,它不是在表單類型中。

這是默認的驗證約束:

public static function loadValidatorMetadata(ClassMetadata $metadata) 
    { 
     $name_blank  = new NotBlank(); 
     $name_blank->message = "The name should not be blank"; 
     $metadata->addPropertyConstraint('name', $name_blank); 

    } 

有沒有辦法覆蓋Symfony的默認驗證?

public function checkRequiredDynamicFields(ExecutionContextInterface $context) { 

$metadata = $context->getMetadata(); 

if (empty($this->name)) { 
    $context->buildViolation('This is a required field.') 
     ->atPath('name') 
     ->addViolation(); 
    $context->buildViolation("Missing the title for conference section.")->addViolation(); 
} 


} 
+0

你能提供你的實體和你的FormType? – OlivierC

+0

這個問題並不清楚,但是您可以查看此http://symfony.com/doc/current/reference/constraints/Callback.html#the-callback-method來驗證自定義行爲。 – yceruto

回答

1

在構建形式,考慮增加'mapped' => false的額外字段:

$qb->add('unbound_field', null, array('mapped' => false)) 

如果你想以後添加自定義的驗證,看看this question

+1

我不認爲他在找什麼。到目前爲止,我的理解是,他對某個實體的財產存在一些驗證限制。 那些屬性沒有出現在formType上,並且當他想要檢查表單時觸發約束條件是有效的 – OlivierC

+0

@Stevan,你能否重新表達你的問題以消除混淆,Olivier有一個好的觀點。 – inaliahgle