forms posts

Symfony 2.0 and Custom Validation for Entity Based Forms

I recently started using Symfony’s form system at work instead of dealing with the forms entirely manually. I had found it confusing at first and so was reluctant to start using it, but now that I’ve started to figure it out, it’s proving rather nice. I still haven’t figured out how to create form classes or deal with subforms or any fancy stuff like that, but it’s helped reduce the amount of work effort per form and normalize the way we handle each.

Forms for entities are nice and do a lot automatically. However, I did run into a problem with them in dealing with a multi-site system: They can’t currently have custom validation applied to them. The constraints are specified in annotations in the entity or YAML or other configuration files, which I don’t think will easily accommodate being overridden for an individual site’s needs. We have many sites that have similar functionality that we want to share between them but some need things slightly different. To accommodate a site having custom validation, I created a form service with a function to handle custom validation constraints. The function looks like:

Continue reading post "Symfony 2.0 and Custom Validation for Entity Based Forms"