Dynamic Form Validation in Symfony using Ajax

Symfony has a great form validation system. The only downside is that it requires the form to be submitted in order to check for any constraint violations. And sometimes, that means that user experience will suffer. And other times, a form submission only for error detection is just not possible. For example, I like to open small forms in modal windows. In this case, submitting the form means closing the modal. And detecting the error and re-opening the same modal with the new information is a painful process. In order to handle all these cases elegantly, I have a small Ajax routine which dynamically checks form constraints while the user completes the form. I still have server-side Symfony validation in place, because one can easily get around JavaScript verification. Read More

Creating and Using Services in Symfony

Services are little useful bricks that can be used almost everywhere in your Symfony application. If an object needs to be used in various Controllers then it should be written as a Service. In this short tutorial I am going to describe a simple implementation of Services, explain how to access the entity manager and other Symfony components inside a Service, and show a few examples of how Services can be called and used in Controllers and in Commands. Read More

Custom error pages in Symfony

We all know the annoying guy in the image. It tells us that something went terribly wrong. In order to see him as little as possible, I have become a control freak : I check everything in the Controller. Even the things that I know are not supposed to happen. But if someone tampers with some data in the database and those never-to-happen things do occur ? I mean, everyone has seen famous mysql errors on big important websites. So bad data does happen. So in order to handle all my this-code-should-not-be-reached tests gracefully, I have a custom made error page. Read More

Twig Macros

Macros are to Twig what functions are to PHP. They help the developer reuse certain repeatable elements, and they give an additional clean and organized look to the final code. So here is a quick an easy Macro tutorial for Twig.

Aims of this tutorial :

  • Create a simple twig macro
  • Include it and use it in a template
  • Create a second macro that makes use of the first one

Read More

Easy Multiple File Upload in Symfony using the CollectionType Field

File uploads have always been tricky, and handling them gracefully and painless is a challenging task. Avoid duplicates, handle removal and replacing without leaving orphan files somewhere in the database or on the server, showing a nice interface to the user, avoiding the ugly standard grey upload button … a lot of things need to be taken into account when working with file uploads. Luckily, Symfony has some great mechanisms which allow easing the task. In this tutorial I will be describing how I handle multiple file uploads using doctrine events and collection type form fields.

This scenario for this tutorial is a simple one : Folder and Document management. A Folder may contain a number of Documents, whereas a Document can only belong, and thus be uploaded, to a singe Folder. At any time during the Folder creation or modification, we need to be able to add or remove Documents from the Folder.

Read More