1

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.')
        ->atPath('name')
        ->addViolation();
    $context->buildViolation("Missing the title for conference section.")->addViolation();
}


}
awenkhh
  • 5,811
  • 1
  • 21
  • 24
Stevan Tosic
  • 6,561
  • 10
  • 55
  • 110
  • Can you provide your Entity and your FormType ? – OlivierC Nov 24 '16 at 10:19
  • The question is not clear, however you take a look at this http://symfony.com/doc/current/reference/constraints/Callback.html#the-callback-method to validate custom behavior. – yceruto Nov 24 '16 at 13:16

1 Answers1

1

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

LdiCO
  • 577
  • 12
  • 31
  • 1
    I don't think this what he is looking for. What I understand so far is he has some validation contraint on a property of an entity. Those property does not appear on the formType and the constraint is triggered when he want to check the form is valid – OlivierC Nov 24 '16 at 10:22
  • @Stevan, can you reformulate your question to erase confusion, Olivier have a good point. – LdiCO Nov 24 '16 at 10:49