Adding new objects to a form field collection

homer-dohI couldn’t think of a good title for this post. So, erm, sorry about that.

Still, if you are reading on, this may save you a headache in the future.

I came across an issue today where I was basically doing this (adding a new option at run time to a form), and it worked.

Kind of.

It would always add in the first option.

And it would add in the second option too, should I add one.

But any above the second were always lost. I couldn’t figure out why the submitted array only ever contained, at most, two items.

Out of sheer luck, I also had another collection sub form inside the very same form, which I had basically copy/pasted from.

Now, this made the whole – why the *expletive deleted* isn’t this thing working?

Turns out, I had set my prototype_name in my Form Type to something different to that in the .text().replace(regex) in my javascript.


Still – at least I know for the future. And hopefully, if you ever make a similar mistake, you will now know how to rectify it quickly.

Have you ever used Composer?

logo-composer-transparentYou have, right?

Like all the time. Ever since you found a framework no doubt. I mean, ever since I found Symfony 2, I have used and loved Composer.

Stuff like this amazes me. That the open source community can provide such an incredible feature just for the love of creating helpful software just blows my mind.

But I digress.

If you do use Composer (we already established that you do, right?), then no doubt you have heard of Packagist. Another amazing site / tool / play on words.


Just this evening I needed it for finding a suitable payment processing class.

Now, it comes back with tons of results. That’s always a great start.

But critically – it doesn’t let you sort the results. Boo. Hiss.

And also, I wanted to send them / him / her a quick bit of feedback, but they (him/her?) want to open my default email client to do so. It’s 2013, get a contact form!

But aside from those two little niggles, it’s such an amazing site. And it helped me find a great payment processing bundle.

Amazing enough to make me write a blog post about it. So it must be doing something right.

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.

Where did all my disk space go?

can-of-wormsI hit a snag this week on my dev box.

The snag being, I was using 100% of the space on my VM.

How? How could this be? I had a single Symfony site, a database, and neither were that big.

But development had been going on a while, and I had noticed on ssh’ing in, that Ubuntu was telling me that my disk space was slowly but surely diminishing.

Silly me for only setting up an 8gb VM.

But still, a little strange.

Anyway, I did the usual – php app/console cache:clear – which got me some space back, but not much.

I added a new disk in VMWare and followed the GParted Live CD instructions, but then it turns out that won’t work for VMWare. You have to extend the LVM (or something!) and frankly, I didn’t have time nor the inclination to do any of that.

Still, the cache:clear had got me a little bit of breathing room. Enough to allow me to work through yesterday.

Then today, I was dropping and recreating the schema, and lo-and-behold, the disk ran out of space again.


me@my-ubuntu:/var/www/html/mySite$ df
Filesystem                   1K-blocks    Used Available Use% Mounted on
/dev/mapper/ubuntu--tlf-root   6946528 6593648        12 100% /
udev                            505076       4    505072   1% /dev
tmpfs                           205056     292    204764   1% /run
none                              5120       0      5120   0% /run/lock
none                            512636       0    512636   0% /run/shm
/dev/sda1                       233191  139705     81045  64% /boot


Then I figured, well, why not manually kill the entire cache directory and see where that gets me:

me@my-ubuntu:/var/www/html/mySite/app/cache$ sudo rm -rf *
me@my-ubuntu:/var/www/html/mySite/app/cache$ df
Filesystem                   1K-blocks    Used Available Use% Mounted on
/dev/mapper/ubuntu--tlf-root   6946528 5081080   1512580  78% /
udev                            505076       4    505072   1% /dev
tmpfs                           205056     292    204764   1% /run
none                              5120       0      5120   0% /run/lock
none                            512636       0    512636   0% /run/shm
/dev/sda1                       233191  139705     81045  64% /boot

Let’s just say the machine made some seriously grunty sounds, but it did the job.

Still, I think next time I will go for a 16gb drive just to be sure.

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.

How to render just the CSRF token in Symfony 2

forged-moniesIt’s not often – thankfully – that I need to customise a form to the extent where I can’t use the lovely {{ form_rest(myForm) }} functionality, but when that situation arises, and you still need the CSRF token, you may think you need to go into your form type and add it in there, in some weird hidden field.

The manual page is quite deceptive in that it helps perpetuate this viewpoint.

Thankfully the solution is very simple, simply put: {{ form_row(form._token) }} in your twig template and Symfony’s form creator and rendering processes will handle the rest.

I can’t take the credit for this one, so here’s my original source of knowledge.

Trouble moving your Symfony 2 install to a new server?

nail-biting-nerdI just moved my biggest Symfony 2 site to a new server, and in doing so I made a couple of discoveries.

Discovery #1 – that .git folder? Pretty important.

Yeah, I thought I would save myself the megabytes by not uploading the .git folder. Oopsie.

Turns out that composer throws a fit if you don’t. My bad.

Easily rectified, right? Just upload the damn folder. Ooh the megabytes.

Discovery #2 – that vendors folder? Pretty useless.

The megabytes I saved not uploading the .git folder I figured were well spent on uploading the vendors folder.


I ended up getting a funky error:

 Failed to clone via git, https and http protocols, aborting.
- git://
 fatal: No such remote 'composer'

When trying to run composer update.

In the end I had to delete the entire vendors folder (sudo rm -rf vendors/) and re-run composer update.

The conclusion? So many wasted megabytes.

But hey, bandwidth is cheap, right?

Setting up a Debian server for hosting a Symfony 2 site with Apache 2

This is more a guide for me than for the Internet at large, so if you do find this, and you find it useful, excellent.

  1. Install to shell
  2. Check network connectivity – ifconfig
  3. su
  4. Edit /etc/apt/sources.list , remove references to CD Rom
  5. aptitude install sudo
  6. add {user} ALL=(ALL) ALL , under the root line
  7. exit
  8. test with sudo apt-get update
  9. sudo apt-get install git curl acl vsftpd
  10. curl -s | php
  11. sudo mv composer.phar /usr/local/bin/composer