Stevan Tosic Stevan Tosic - 1 year ago 154
PHP Question

Symfony How to remove constraint from default validation group

I have form type with some data from entity.

There is a field that is required and it's is not in the form type.

This is the default validation constraint :

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


Is there a way to override Symfony default validation?

public function checkRequiredDynamicFields(ExecutionContextInterface $context) {

$metadata = $context->getMetadata();

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


Answer Source

When building the Form, consider adding 'mapped' => false to the extra field:

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

If you want to add a custom validation afterward, take a look at this question