Symfony2 form theme for Bootstrap

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!
Introducing TinyPop
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.
Lightweight Javascript inline editing
Update 2: Core methods rewritten for compatibility with browsers. Check change log for details
Update 1: Script updated considerably
There are many excellent scripts to edit something inline using javascript (like jeditable), but not many that work standalone. If you hate wanting to include a 50 KB library just for this simple functionality, you are in the right page. This script is tiny (1.7 kB minified) and works on most modern browsers
Steps:
1) Include the editable.js file in your page
2) Add the attribute class="editable" to a division, paragraph or table cell <td>
3) Click on the element for it to turn into a text box. Update the value, and press enter to save. Press tab or click anywhere on the page to discard updates and revert to the old value
Changelog
Version 0.2
- Support for all major browsers
- Table cell editing more robust
- Added support for IE 7 and 8. Uses ie.js for support for getElementsByClassName
Initial Release
- The editable box now fits to the size of the original element. You can see that in the demo
- Support for inline table cell editing is now included (Proper layout/orientation only work in FF for now. Horribly out of place in other browsers and will be fixed later)
- Editing of an element with other HTML tags now works better
Gotchas:
1) Only one element is editable at any instant (I wanted this specific feature for a project I'm working on)
2) The text box doesn't fit to the size of the original element (will be fixed)
Tested On:
Windows - Firefox 3.6, IE 7 & 8, Chrome
Mac - Firefox 3.6, Safari 4, Chrome
Terms:
Free to use for personal or commercial purposes, but please give credits
Notes:
I know it can always be better, and I'm just a beginner. So comments, suggestions always welcome!
MAC/IP address using Linux shell script
Ever wanted to get the MAC/IP address of your computer in a Linux shell script? The following two commands should work on most flavours of Linux/Unix
Note: It retrieves the address of eth0 interface by default. Make necessary changes as required.
IP:
/sbin/ifconfig | grep '\<inet\>' | sed -n '1p' | tr -s " " | cut -d " " -f3 | cut -d ":" -f2
MAC (H/W address)
/sbin/ifconfig | grep 'eth0' | tr -s " " | cut -d " " -f5
Ubuntu’s Karmic Koala
So begins the battle between the Operating Systems again. And this time a much fierce battle I guess. With Microsoft Windows 7 and Apple's OS X Snow Leopard 10. 6 already rumbling in the arena, there enters Ubuntu 9.10 codenamed Karmic Koala.
Ubuntu's 9.10 promises to be faster and friendly to use (no more scarry text scrolls) and also is introducing newest version of- Linux kernel - Version 2.6.31
GNOME - Version 2.28
Firefox - Version 3.5.3
Software Center - graphical utility for package management
GRUB2 as the new default bootloader
Ext 4 filesystem default
But, the best part of Karmic Koala is Ubuntu One, a new web sync feature that gives you 2 GB of free online disk space and also allows you to syndicate and synchronize contacts, notes and share files between several computers. Yes! I know! I can already see people ruminating from Windows. Wait! There's still more to come.
You can chat to your friends and your office colleagues through Empathy which integrates: Yahoo, Gmail, MSN, AOL, Jabber and many more. There is F-Spot for you through which you can manage and upload your photos to photo-sharing sites like flickr, facebook, picasa and what not!
Create your presentations, documents with Open Office v 3.0 which is quite compatible with all other available office applications including the most famous Microsoft Office. Access Last.fm, Internet Radio portal directly using Rythmbox.
Cannonical COO Jane Silber quotes "Ubuntu 9.10 gives users more reasons than ever to seriously consider Linux at a time when many are thinking again about their operating system options. We are delivering a platform for users interested in an easy-to-use, great-looking, web-friendly operating system".
The average boot time is about 26 seconds with the Xorg showing up around 15th second. The eventual goal is to deliver a 10 second startup. Probably, we can find one such thing happening in 10.04. Software Center has an excellent selection of software, revamped audio framework improves sound control across multiple applications and many new and improved features.
At the core of the Ubuntu philosophy is the belief that computing is for everyone and whatever your circumstances.
