Symfony2 – Getting All Errors From a Form in a Controller

Recently I was working on an API call which uses a form to validate the data you are passing in. I ran into the issue of getting all the errors for a form in Symfony2 is not as straight forward as you would think. At first I tried `$form->getErrors()` and then looped through the errors. That didn’t get all of them. After looking around I found:

This works really well for any errors which are bound to a field. However it will not catch global errors such as a unique validator. It should probably be renamed from getAllErrors(). In order to get those you need to also loop through $form->getErrors(). This was returning the global errors only for me. Here is my code in the end:

There may be a better way, just wanted to shoot this out as not many people had good solutions on it.

Bonus: If you are using the translator service on validators and you get an error which is the ‘validators’ translation files, make sure you use the proper domain, ie: $translator->trans(‘key’,array(),’validators’).

AJAX Request Slow With PHP? Here’s Why

Recently I was working on a project where we had a page which loads tons of data from numerous sources. I decided after a while that we wanted to AJAX each section of data so that the page would load a bit quicker. After splitting up the requests and sending them asyncronously, there was little improvement. I thought at first it may be due to the fact we were pinging a single API for most of the data multiple times, that wasn’t it. Maybe it was a browser limit? Nope was still far below the 6 requests most allow. I setup xdebug and kcachegrind and to my surprise it was the session_start() that was taking the most time on the requests.

I looked around the web for a while trying to figure out what in the world was going on. It turns out that PHP’s default session_start will block future session_starts for the same session until the session is closed. This is because the default method uses a file on the filesystem which it locks until you close it. If you want more information on this and how to close it you can read a bit more here.

We switched over to database based sessions and it fixed it. In symfony 1.4 the default session storage uses the file system, however switching over to sfPDOSessionStorage is very easy and quick.

Red Wings & Michigan Lottery cross promo launched!

Hope everyone survived the first week of 2012 unscathed. Just wanted to announce that we helped the Detroit Red Wings and the Michigan State Lottery launch a Facebook promotion. It’s a pretty neat deal, all you have to do is LIKE the MI Lottery page and submit your information to be entered to win a Zamboni ride. And honestly, who doesn’t love a Zamboni ride.

The promo is live here https://www.facebook.com/MichiganLottery

Unfortunately, its only open to Michigan residents.

Boomerang via @fredwilson and @cdixon

Hope everyone had a great Christmas and that you’re gearing up for a great New Years!

Anyway, I picked up a Kindle Fire for Christmas and decided to use it as an eReader at least once in its life. I had remembered seeing Boomerang by Michael Lewis recommended by both Fred Wilson and Chris Dixon so I figured it must be worth checking out.

The book is a short, easy read but it offers a fascinating perspective on the global financial crisis through several different viewpoints. As Chris pointed out, theres also a couple of funny anecdotes and bizarre cultural highlights. If you have some time to kill, I’d definitely recommend picking up Boomerang and giving it a once over.

Toggle non-consecutive checkboxes with jQuery UI

You’re all probably familiar with the UI convention of allowing users to select ALL or NONE for a list of checkboxes (like in Gmail). Recently I was working on a project that had a large table full of checkboxes (imagine a 10×10 grid) where the user would need to toggle some but not all of the checkboxes in a given row. And to make matters more complex, they would need to toggle groups of non-consecutive checkboxes (say 15, skip 10, 5, etc.). I threw on the thinking cap but couldn’t think of any similar interactions I’d seen and couldn’t think of a particularly good way to achieve this.

Enter jQuery UI. I happened to stumble across the jQuery UI Selectable documentation and realized it would provide a good UI experience to toggle some but not all of the checkboxes. The code to implement this is surprisingly simple:

Note: You don’t actually need the div container – that was just for JSFiddle.

And then the Javascript (jQuery + jQuery UI):

You can check out a live demo at http://jsfiddle.net/whMyQ/3/

As always, questions and comments are welcome!