Testing Symfony 2 Forms with Multiple Checkboxes

checkbox-gridThe thing I find most frustrating about Symfony’s documentation is that it always seems to fall just shy of getting me over the finish line.

And those extra few yards can take an absolute age to travel.

I needed to test a checkbox field – not the actual ‘checkbox‘ field type, rather an entity field type inside a collection that was rendered with checkboxes (multiple => true, expanded => true).

Three cheers for making that one confusing.

There’s a nice piece inside the dom crawler documentation that kinda explains how to do this, but not exactly:

// check or uncheck a checkbox


Well, great that is until you realise your form looks more like this:

// check or uncheck a checkbox

That is – you have maybe 5 checkboxes and they all have that exact same html form field name, but a slightly different ID.

So I tried the ID. No dice.

Then I tried to use the $form->setValue method described in the documentation. Turns out that method is not allowed – not sure why that’s still on there.

Then I found a poor soul who had been through the same trauma I just experienced, over at that wealth of obscure knowledge, Stack Overflow.

Craig, being the good man that he is, not only posted up the problem, but replied – the only reply I might add – with the solution.

It’s people like Craig who make the Internet a better place.

Anyway, for those of you who can’t be arsed clicked all these links I have so helpfully provided, the solution is as follows:

// check or uncheck a checkbox

Where the number is the zero index of the array of buttons you are trying to click.

After that it works like a PyCharm.

What are they Allowable Values in a Symfony 2 Currency Field Type?

500-leonesI love Symfony 2, don’t get me wrong, but the documentation? Lacking.

I know the documentation is always improving, and that essentially no one likes to document when they could be coding, AND that they are always asking for volunteers to help improve the documentation.

The thing is, how do you know what documentation is missing, until you stumble across it during your day-to-day Symfony-ising?

Well, today was one of those days.

I just happened to be using the Money field type for first time ever. And I wanted a few different currency symbols to be made available to my end users.

All good?

Well, not quite.

The documentation lists the default option of Euro. But as we all know, the UK will never enter the Euro. At least, not on my watch.

But what other values are there?

Well, GBP seemed like a good choice. And it’s there. Great.

And there are a few hundred or so more. But where to find them?

Try here.

Just look at that path, Christ on a bike.

Still, at least now you can count all the Albanian Lek your users can throw at you.

How to use Raw SQL Queries in Symfony 2

hashtag-doctrine-dogSometimes, usually when you first start with Symfony 2 (but there are other times too), you just want to get access to good old raw SQL.

I have experimented with a few different ways, and – as with many Symfony issues – the documentation is either shonky, or worst, so cryptic it requires a Mensa-like IQ level to decode just WTF these guys are talking about.

Now, if you are new to Symfony 2, before you go about using raw SQL for everything, do yourself a favour and make sure you learn Doctrine.

And then only if you absolutely must go native SQL, then use the following:

    public function foobar($foobar)
      $stmt = $this->getEntityManager()
                   ->prepare('SELECT COUNT(id) AS num, foo FROM bar WHERE foobar = :foobar GROUP BY foo');
      $stmt->bindValue('foobar', $foobar);
      return $stmt->fetchAll();

I love this, and I have used it with great success!, but the credit is not mine, it belongs to a user called althaus on the old symfony forums.