I-Am-Bot Code, technology and life

21Nov/110

Symfony2 form theme for Bootstrap

Posted by Srinath

logo_symfony_header

Symfony2 is a flexible, fast and secure PHP 5 framework for developing modern applications. This post assumes you already know the basics of working with Symfony.

Symfony2 has a very powerful templating engine in the form of Twig, a simple templating engine much like the popular Mustache. Twig allows non programmers to quickly design HTML layouts without the need to write PHP code. It also makes working with forms a breeze, and all that is needed to output a form on a template is

form_widget(form)

However, styling the form to suit your need takes a bit more effort. Symfony does allow your application to have different form themes which can be then be applied to various forms. It involves overriding various blocks to render different parts of the form. The problem is in finding which blocks to edit, and which to leave alone. This post can serve as a theme to render forms styled like the ones in Bootstrap.

When drawing a form using the form_widget(form) or form_row(form.field), Symfony uses the default theme that is defined in the file form_div_layout.html.twig. This file contains the various block definitions that are called when you use the appropriate twig function. For example, when you simply call form_widget(form), you can see how it in turn calls field_rows(form), which then renders the form errors, and for every field in the form, calls the corresponding widget block.

For Bootstrap's theme, we will override 3 blocks, and define one macro to display errors from the form.

We first define the macro hasErrors to check if the current field has an error, in which case the string "error" is returned which is appended to the class of the input div for the field to be rendered as an error field

/src/Acme/DemoBundle/Resources/views/Form/macro.html.twig

{% macro hasErrors(field) %}
    {% if form_errors(field)|length > 1 %}
        {{ 'error' }}
    {% endif %}
{% endmacro %}

Then, we redefine the field_rows block and do not display the error messages at the beginning of the form

/src/Acme/DemoBundle/Resources/views/Form/theme.html.twig

{% block field_rows %}
{% spaceless %}
    {% for child in form %}
        {{ form_row(child) }}
    {% endfor %}
{% endspaceless %}
{% endblock field_rows %}

Next, we define the custom field_row block that actually renders the HTML for each field. We define the container div for each input field, and call the hasErrors macro to check if Symfony has returned an error for that field. If yes, the class "error" will be added to the class attribute, and the entire field will be highlighted in red to indicate an error. We then echo the error after the field, just like in Bootstrap. Notice how we are passing the global _context object to the form_widget block. By default, you can pass custom attributes to the form_widget block and not to the field_row block. This is done so that we can add custom attributes to each input field, as shown in the example at the end.

/src/Acme/DemoBundle/Resources/views/Form/theme.html.twig

{% block field_row %}
{% spaceless %}
    {% if macro is not defined %}
        {% import 'AcmeDemoBundle:Form:macro.html.twig' as macro %}
    {% endif %}
    <div class="clearfix {{macro.hasErrors(form)}}">
        {{ form_label(form) }}
        <div class="input">
            {{ form_widget(form, _context) }}
            {{ form_errors(form) }}
        </div>
    </div>
{% endspaceless %}
{% endblock field_row %}

We have defined the actual macro, but we now have to customize the error display. That is done in the field_errors block

/src/Acme/DemoBundle/Resources/views/Form/theme.html.twig

{% block field_errors %}
{% spaceless %}
    {% if errors is defined and errors|length > 0 %}
        {% for error in errors %}
        <span class="help-inline error" style="float:right;width:170px;margin-top:5px;">{{ error.messageTemplate|trans(error.messageParameters, 'validators') }}</span>
        {% endfor %}
    {% endif %}
{% endspaceless %}
{% endblock field_errors %}

That's it! We have now created a custom form theme! To use this, just include the following twig block before drawing the theme

{% form_theme form 'AcmeDemoBundle:Form:theme.html.twig' %}
{{ form_row(form.name, { 'attr': {'class':'xlarge','placeholder':'Please enter your name'} }) }}
{{ form_row(form.description, {'attr': {'class':'xlarge'} }) }}
{{ form_rest(form) }}

We are using the form_row block to render each field, so that we can also pass along custom attributes to each field, just like the form_widget block {{ form_widget(form.task, { 'attr': {'class': 'task_field'} }) }}. However, if we use form_widget(form), then custom attributes cannot be passed to individual fields, but the theme will be rendered nevertheless. Passing custom attributes is particularly useful to adjust the width, height, placeholder, etc of the fields.

That's it! You can define various themes for different parts of your application in a similar fashion, and simply set the appropriate theme with the form_theme directive before rendering a form. Have fun theming forms in Symfony2!

Tagged as: , No Comments
12Aug/112

Update on phpUserAuth

Posted by Srinath

My apologies for the lack of activity on phpUserAuth and a couple of other scripts. I have decided to spend sometime each week on my small projects, and to answer comments and questions regularly. As a first step towards that, phpUserAuth is now available on github. The project will be hosted there for better code management and it also makes it easier for other devs to contribute or customize it for their own needs. I realize that the code needs a lot of work, and some parts of it will have to rewritten from scratch. The immediate need is to cleanup some code, and improve the file organization. The commented JS source files will be uploaded and reorganized.

And to anyone who is using/planning to use it, I would like to know the most important feature that is needed immediately. That, along with enhancements to the admin page will be worked on. If anyone has already worked on enhancements, please put in a pull request on github.

Here's hoping for regular activity on the project!

Filed under: code, projects 2 Comments
14Nov/100

php-znisms released

Posted by Srinath

php-znisms is a PHP API for the SMS services provided by ZNISMS (currently only for India). This API can be used to send single or multiple messages, and check the account status.

Requirements
PHP version 5 or higher with support for CURL and SimpleXML

Usage
Detailed guide coming soon. Please see the file test.php for details on how to use it

Download

Tagged as: , , No Comments
28Aug/100

Introducing TinyPop

Posted by Srinath

 

TinyPop is a lightweight (1.5 KB minified) JavaScript for Growl like notifications. It can be used as an alternative to jGrowl when you don't want to use jQuery for a simple task.

Details

Download

Demo

20Jul/1016

phpUserAuth Released!

Posted by Srinath

It's finally done! The package is up for grabs, with a quick starter guide.

Details

Download

Demo

Please go through the project page for more details!

The Readme.txt file inside folder "readme" contains instructions for installation and usage

Leave your comments/questions/suggestions on the project page.

Out.