Chris Worfolk's Blog


Symfony2 custom validation constraints

June 15th, 2012 | Limited, Programming

The Symfony2 framework comes with a really nice validation library and on top of the built in constraints that you can use you also have the ability to add your own custom constraints to perform any other validation checks you need to do.

The Cookbook has an article on how to create such a custom constraint, but unfortunately, as has happened a few times with the Symfony2 documentation, it misses out some fundamental and sometimes rather obscure step in order to get it working.

The problem is, it doesn’t tell you where to put the files. That leaves two options – either you have somewhere they need to be in order for Symfony to find them, or you need to fell Symfony where they are.

After hours of digging around, we eventually found the answer – in your validation.yml file you need to register the namespace and then reference that name when you call the validation rules.

Start by registering the namespace at the top of your validation.yml file.

namespaces:
  myValidator: Acme\ExampleBundle\Validator\

Lets say you have added a custom validation constraint called Postcode. When you want to invoke that from your validation rules, you need to reference the namespace you have just created.

postcode:
    - myValidator:Postcode: ~

You can then use this namespace with your custom validation rules, place them in the appropriate directory (in this case src/ExampleBundle/Validator, but you can store them somewhere else if you change the namespace – remember that Symfony2 will extract the path from the namespace).

The Future of PHP

June 14th, 2012 | Events, Programming, Tech

Last month, I went to a talk by Richard McIntyre on “The Future of PHP”.

Turns out, it’s JavaScript.

More and more these days, web applications are being developed with fat front end clients loaded with JavaScript, and the server-side processing is primarily used for data processing and APIs. So we’re seeing a shift from PHP being used as a somewhat front-end technology to merely (I say merely, there is a lot of work to do it) delivering the content in a format the JavaScript front end can consume.

Perhaps this is why we’ve seen a rise in the number of microframeworks, such as Silex, that I recently blogged about.

I think the overriding message I took away from the talk though is that they’re inventing development platforms faster than I can learn them! I’ve already got a large list of technologies and libraries I need to review and I think I came away from this talk with another half a dozen!

Really enjoyed the talk though, and if you’re interested in PHP and in the Leeds area you should definitely check out the Leeds PHP User Group who host such events on a monthly basis.

Creating a user in Unix

June 13th, 2012 | Life, Tech

Back to basics today. Creating a user and setting their password.

useradd test
passwd test

Juice

June 13th, 2012 | Photos

Juice

I tried to buy some juice from Sainsbury’s. Turns out what I actually bought was juicy water.

Found a Finder

June 12th, 2012 | Photos

We see so many broken displays showing their Windows backdrops, it’s a refreshing change to see a Mac one.

Finder

Btw, does everyone else still remember when Bed used to be Gatecrasher? I’m so old…

May 2012 Wendy House

June 11th, 2012 | Life, Photos

While a great deal of fun was had at last month’s Wendy House, I arrived there to find that my camera’s battery had in fact died. So all I have is one photo that Hugh kindly took with my phone…

Hugh

Little London

June 10th, 2012 | Photos

Little London

I always knew that the are just north of the city centre that is exclusively reserved for crack addicts and muggers with knives was called Little London. But I think I must have thought it was a disparaging nickname that everyone gave it. It never really clicked that that was it’s actual name.

Eurovision 2012

June 9th, 2012 | Distractions

How can we sum up this year’s Eurovision in one word? Disappointing.

Of course, it was never going to end well. When you allow a country which bans homosexuality, each country was forced to field a heterosexual Eurovision act for the first time in the history of the competition, and the results were disastrous. Compare this year to last year’s performance by Getter Jaani and you can see an instance difference.

Even Moldova, who last year provided us with a fairy on a unicycle curtsey of Zdob si Zdub miserably failed to re-create last year’s magic.

To finish it all off, we came second to last. People just didn’t get behind The Hump. No wonder; he has a great voice but simply isn’t Eurovision material. Why we keep picking this acts when Holly Johnson is still alive, I simply can’t understand!

Eurovisoon 2012

Silex

June 9th, 2012 | Limited, Programming

Silex is a PHP microframework based on Symfony2 components.

With the shift in recent years to leveraging more JavaScript and front-end code in fat clients, a lot of server-side processing has been reduced to simple data relay and APIs. As a result, there have been a number of microframeworks arisen, which allow you to serve out content in a really simple and easy way.

One of the most popular is Sinatra, a micro-framework for Ruby, which is what we built Village Chief on. Indeed, Silex is inspired by Sinatra, but is PHP-based and uses some of the great components that can be found in the Symfony2 framework.

As you would expect from a microframework, it’s really easy to get started.

<?php
require_once __DIR__.'/../vendor/autoload.php';

$app = new Silex\Application();

$app->get('/hello/{name}', function ($name) use ($app) {
    return 'Hello '.$app->escape($name);
});

$app->run();

It relies heavily on Composer, a PHP dependency manager. This is a bit of a pain if you’re not already using Composer as it means you have to have yet another piece of software on your computer, but unfortunately, you’re somewhat railroaded into it as there is virtually no documentation on how to install things like Twig without it. Luckily, once you have it, it does make things easy and pain-free, so it’s probably worth going through the initial setup.

Once you’re up and running, it’s a snap to add content. We recently re-launched Maze Finance and the entire process of getting Silex up and running and migrating our existing website into it took less than two hours!

Religions and cults

June 8th, 2012 | Religion & Politics

Recently, The Big Questions aired an hour long one topic episode asking “is there a difference between a cult and a religion?”

Of course, there is a difference – size. If you’re a large organisation you are described as a religion, if you’re a small one, you’re described as a cult. That is the sarcastic way of saying there is no difference. Which was the general consensus on the show (both the “cult member” guests they had on, and the impartial guests) with the exception of a few religious figureheads.

The general agreement was that cult isn’t a black or white test, it’s a scale, with lots of different characteristics, of each different groups conform to different characteristics, some to many more than others.

Two of the biggest defining characteristics of a cult that kept coming up in the discussion were child abuse and penalty clauses for leaving. I find these two very interesting as the sticking points for whether an organisation is classed as a cult or not due to how closely the major religions match up to such characteristics.

I’m sure no one needs reminding that child abuse is simply endemic in the Catholic Church. Right up to their leader, God’s representative on Earth, Pope Benedict has been involved in trying to cover up child abuse. But they are far from the only example – both the Muslim and Jewish faiths continue to cut out and mutiliate small children’s genitals[1]. Worst of all – they’re proud of it! They define it as their culture to cut apart a defenceless child’s private parts in the name of religion. It’s physically sickening, and it happens on a worldwide scale.

Shunning those who leave is also equally endemic in the major religions. Just try marrying someone who isn’t Jewish[2] in an Orthodox Jewish environment. It won’t end well for you. Oh, and did anyone forget that the punishment for apostasy in Islam is death[3] [4]?

It would seem that one of the main differences between a religion and a cult is whether a group gets away with it’s child abuse and psychological abuse of its members, past and present.