我有來自實體的一些數據的表單類型。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();
}
}
你能提供你的實體和你的FormType? – OlivierC
這個問題並不清楚,但是您可以查看此http://symfony.com/doc/current/reference/constraints/Callback.html#the-callback-method來驗證自定義行爲。 – yceruto