Symfony2 & TDD – Testing Entity Validation Constraints

I am trying to learn TDD and have had some problems to get started with TDD in Symfony 2 but I have finally stumbled on a solution (I hope, correct me if I am doing something wrong please).

All we need to do is to use the ValidatorFactory that comes with Symfony like in the example below.

        $validator = ValidatorFactory::buildDefault()->getValidator();
        $errors = $validator->validate($e);
        $this->assertEquals(0, count($errors));

I found the solution here so a big thank you to Kasheen.

KnpPaginatorBundle with JQuery partial postback

This post continues the previous post Pagination in Symfony2 with KnpPaginatorBundle.

First of all you should add JQuery to your twig template. I recommend that you do not download JQuery and put it in your assets, but instead include it in your header like this:

That will save you some bandwith and there is a big chance that the browser visiting your site already has the jquery-latest.min.js already in it’s cache.

Partial postbacks will speed up the user experience since we do not need to reload the whole page everytime we change page in the pagination, just the table. So first we need to do is create a new route that can handle the postback and in the same time we need to keep the old route’s functionality in case that the browser client do not have javascript. So lets start with the controller. (more…)

Pagination in Symfony2 with KnpPaginatorBundle

Pagination is always nice in an application. I found a great bundle called KnpPaginatorBundle that helps you with pagination and as an bonus we also get sorting.

So as usual we need to add some bundles to our deps file:




As you can see the KnpPaginatorBundle uses Zend for the pagination. (more…)

Twig and empty date

Ran in to a small problem when I was programming today. I had an Entity Person that had a birthday that could be empty (if we did not know it). When it was displayed with twig an empty value was displayed with the current date.

So to display an empty date with twig you have to do something like this:

{% if entity.birthday %}
    {{ entity.birthday|date('Y-m-d') }}
{% else %}
    No Value
{% endif %}

May the force be with you!

Symfony2 and Translatable example

To set up translatable have a look at my previous post: Symfony2 and DoctrineExtensions.

First lets have a look at the translatable settings from the previous post

// app/config/config.yml
# Doctrine Extensions Configuration
        default_locale: en_us
        translation_fallback: true
              timestampable: true
              translatable: true

The settings above tells that we want to use en_us as our language and that we want fo fallback to that languge if current lange does not exists. (more…)