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
$form['registration[terms]']->tick();
$form['registration[terms]']->untick();

Great.

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

// check or uncheck a checkbox
myForm[q16][innerQuestionOptions][]

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
myForm[q16][innerQuestionOptions][0]->tick();
myForm[q16][innerQuestionOptions][2]->tick();
myForm[q16][innerQuestionOptions][4]->tick();

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.

Frustrating.

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.