0

Currently I am using validation.yml for validations. Everything works fine. But I need to add a validation rule on a specific condition.

I have a selectbox to select a type, and a textbox to enter other_type. I need to make this textbox mandatory if selected type is other.

How can I perform this validation in Controller after form submission?

Saravanan
  • 1,879
  • 2
  • 27
  • 30
  • 2
    Hey @Saravanan, I asked something similar a while ago, see [Use a conditional statement when creating a form](http://stackoverflow.com/questions/11634377/use-a-conditional-statement-when-creating-a-form). The answer is from Bernhard Schussek, who is the master of Symfony form component. – Mick Jan 27 '14 at 07:31

1 Answers1

0

Provided I have an entity that has a property type and an other property other_type, maybe I would solve the problem, by creating a class constraint. You can find the cookbook article about it here. These class constraints are very useful for the situation described by you, as in the ConstraintValidator's validate method you can easily verify the value of the type property, and in case it is set to other, you can check the other_type property. If the validation marks the entity invalid the addViolationAt method of the validation's context could be used, in order to add the error message to the other_type property of the entity. Finally, you can easily set your validator using yml to your entity.