Symfony2 REST gotcha: Unable to bind a request to a form



While experimenting with building an API based upon the FOSRestBundle, I hit a spot of bother trying to bind a simple request to a form.

The form had three fields, username, email and password, matching the properties of its data class. I was testing it using the Postman extension for Chrome, sending POST requests to a /users URI (the Restful way of creating a new user). Here’s the request:

username => myusernameemail => email@host.tldpassword => 12345

In the controller, I bound the request to the form as usual:


After which $form->isValid() promptly failed with an empty $form->getErrors().

I spent a good chunk of time hunting down validation problems in the User entity annotations, experimenting with the CSRF token requirement, disabling the JMSSerializerBundle exclusion policies for the User entity and praying to the Gods of the Source. To no avail. I could only get the form to work by manually binding each field:

$form->bind(array('username' => $this->getRequest()->get('username'),'email' => $this->getRequest()->get('email'),'password' => $this->getRequest()->get('password'),));

By then I was pretty sure this was deep facepalm country. I find the best way to handle these kinds of situations is to take a break, so I carefully closed the laptop, called it a night and walked away.

Sure enough, this morning the solution hit me as soon as I looked at the code. Quite simply, Symfony expects my POST request to have a top-level namespace matching the form’s name. Given the form had the name ‘user’, it was a simple matter of tweaking the request:

user[username] => myusernameuser[email] => email@host.tlduser[password] => 12345

Done and done.

It’s hard to draw a lesson here. On one hand, this wouldn’t have happened if I was using a form rendered by Symfony rather than crafting my request by hand. Since the manual assumes you’ll be doing just that, it doesn’t bother too much with explaining how things work under the veneer of the helpers and that makes life difficult when building an API that’s not going to be consumed by something built with Symfony. On the other hand, maybe I could have started my debugging by having symfony build a valid form and looking at the request it produced. Then again, hindsight is 20/20.

I guess the only sure conclusion is that, yet again, walking away from the code and coming back well rested works wonders.

We are the Scribes of our times


I'm a professional programmer. Whatever else I am, programming pays my bills. Me and my fellow??purveyors of the Source currently enjoy the bounty of an increasingly tech centric economy because we speak Computer.

Something similar went on a few thousand years ago. Records needed keeping, letters needed writing, and only a handful knew the ways of the stylus. Scribes, they were the programmers of their times.

You may think that the world is going to shit. But it's not. Despite what the rants of newspapers and the smartphone bearing 99% would have us believe, these are times of unprecedented peace and abundance. The human condition has steadily improved, will continue to do so in the foreseeable future, and it's all thanks to widely adopted basic literacy.??Literacy multiplies the value a person can add to the world.??Teach everyone how to read and write, and in the blink of an eye donkeys on the street become jet planes in the sky.??

Machines being effort multipliers, programming is like literacy on steroids. Elementary schools everywhere are adding programming classes to their curriculum.??Soon, software will stop being something you have to get a Scribe to write.

There are two conclusions here:

1. Even in a world where everyone knows how to code, quality will matter. A good programmer will always be like a best-selling author: rare and valuable. But generally speaking we're going the way of the Scribe.

2. The world of widespread computer literacy is going to be awesome.

This has not gone well


Last week I attempted to change my daily routine, waking up and going to sleep much earlier than usual. The objective was to consolidate my at-home time, working on personal projects first thing in the morning rather than in the evening. This makes sense because I'm rarely in the mood to be productive after a long day at the office.

I pulled it off for a while, and it was great. Days were much simpler: wake up, work on my stuff, work at the office, relax, sleep. Much better than wake up, work at the office, fret about not having the energy to work on my stuff, don't relax at all, sleep. Knowing that each day I could get work done on my own projects greatly improved my mood, which had a positive impact on my demeanor and productivity at the office too.

Sadly, going out of sync relative to one's significant other is not easy. Readjusting sleep patterns is hard enough, trying to go to sleep while someone still roams around the house is harder still. By the end of the week I found myself??somewhat sleep-deprived. I was also missing my quality time with her.

So now I'm back to old slightly chaotic routine.

Early bird


It’s 6:20am on a monday morning and today I’m starting a little experiment. The goal is to move my daily routine back by two hours so that I can do stuff before having to go to work. To that end I’m waking up at 6am rather than 8am, and going to bed two hours earlier too.

I’m doing this because my daily routine ever since getting a full time job has been very detrimental to my personal projects – and health. Here’s the typical breakdown:

  • 7am Snooze the alarm
  • 8:30am Get out of bed
  • 9:15 am Leave for work
  • 10:15am Arrive in the office
  • 8pm Arrive back home
  • Midnight Hit the sack
  • 1am Sleep

My best hours are at the office, which would be fine if I still had any energy left for doing my own stuff in the evening – but I don’t. It’s embarassing how silly this routine looks when laid bare like this. Little wonder I’m always tired. I’ll write more of my thoughts on why this happens in a future post.

For now, though, it’s time to enjoy my newfound extra time.

On Ubuntu


I'm running Ubuntu 12.04 at work and it's pretty impressive. The user experience takes many cues from the Mac, but it never feels like a copy. Yesterday I took my old toshiba laptop, which was running an older version of ubuntu, and upgraded it to 12.04. Now it feels like something I can actually use.

This is a very special laptop for me, chiefly because I used it to work on other important projects before getting my first mac. So it feels really nice to have it operational again. I might try coding and launching something with it, for old times' sake.

Back to Ubuntu. If you want to give linux a try, do it. Unless you really need to use some specific piece of software that runs only on windows or mac os, Ubuntu 12 is a really polished and friendly OS. I highly recommend it.

Introducing TeDDy, a use-case driven approach for TDD php

Because of my experience as a freelancer and solo founder, I found that test-driven development eases the burden in the later stages of a project when too much of the time is spent debugging stuff that breaks whenever I touch the code. TDD deals with this problem preemptively and is therefore the Holy Grail of software development processes.

… Or is it?

Trouble is, web development these days is all about frameworks. Take a look at the file structure of any project and you'll guess the framework it is built upon before you get any clue about its purpose. This reliance on frameworks has heavily leaked into testing. Even if you're just testing a Symfony2 service, you're still parsing tons of yaml files and booting up the kernel and injecting dependencies and god knows what else before your service is actually tested. This slows each test quite a lot, which is troublesome if your suite runs hundreds or thousands of tests.

TDD is based on the idea that you write your test first, just enough to prevent the suite from passing. Then you implement some code that makes the test pass. Then you refactor that code so that its cleaner and more efficient. In between these steps you should run your test suite. An entire "write test, run tests, write code, run tests, refactor code" (the Red-Green-Refactor cycle) should take no more than a couple of minutes, which is difficult when justs running the tests takes an hour.

The answer is moving application logic away from the framework. Uncle Bob says it better than I ever could:

Inspired by this talk, and having seen first hand the problems with the test-later approach, I've been doing some experimenting with the idea of a use-case driven approach in php which dispenses with frameworks. I just put on github what I've come up with so far. Called it TeDDy, after the TDD acronym, and I'm using it in my current project, which I started this week. Right now it's running all its tests (30+) in less than a second. Nice.

There's a lot of potential in this idea.

"Job security" only worsens the employment crisis

I was involved in a software project, recently, which had to be shut down. Basically there was a very tight deadline and not enough programmers.

Portugal, as is thoroughly documented in the media, is in a deep unemployment crysis. People are scared. Those with jobs desperately try to cling to them. Many beseech the government to take employment protecting measures. When the government went and made it a tiny bit easier to fire people instead, it caused a bit of a fuss. Of course it did. People want more job security, not less.

Meanwhile, companies have a hard time recruiting good talent. Unemployed people usually suck, employed people are usually prevented from changing jobs with less than two months’ notice. The law which forces companies to give two months notice to a worker being terminated also forces said worker to give the company two months notice when switching jobs. It’s a double edged sword whose only practical result, as far as I can tell, is to make everything move a lot slower in the job market.

A company in need of hiring good talent quickly finds itself out of options. My old boss will probably end up outsourcing the development work to eastern europe or southeast asia and another crumb will be taken out of the portuguese economy.

It’s no secret that a weak economy means less jobs.