Symfony Simple and Secure Ajax Call for Checkboxes

Almost a year ago, I wrote one of my first blog articles, explaining how to use ajax to validate form fields before submitting the actual form. Ajax is a great way to improve user experience, but one needs to be extra careful not compromise the security of a webpage. So here is a simple and secure way to use Ajax for tasks like saving user preferences, validating choices and many more. Read More

DataTables Server-side Processing in Symfony

I love DataTables library and the infinite possibilities it offers. And I have been happily using it for years for small and medium projects. However, when working with large amounts of data, JavaScript handling of the entire data is just not possible. The official documentation talks of slowdowns when dealing with “many thousands or millions of data rows”. However, in practice, I have found that a few hundred is enough to cast a shadow on that perfect user experience we all aim for. So this is where the “Server-side processing” comes in handy. Instead of handling all data operations (fetch, sort, search, paging) on the client side, the DataTables library offers the possibility to perform these actions on the server side and return to the client only the relevant and visible data. The DataTables library has a server-side processing feature which allows the client to make an ajax call to the server with specific parameters and then render the response. This tutorial explains one way of handling this server processing in Symfony.

Read More

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

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. Read More

French Towns, Departments and Regions (January 2018)

A few weeks ago, a project I was working on needed a list of French towns, sorted by department and by region. So I went looking into the official data, and I found a list of towns with names, postal codes, gps coordinates, departments, but no regions. And a list of departments. And another list of regions, with their corresponding departments. A few hours later, I compiled all this (non developer friendly) lists into three big SQL insert queries, corresponding to the following schema :


town (id, name, postalCode, department_id #)
department (id, name, postalCode, region_id #)
region (id, name, code)

Read More