Latest from the Bamboo Blog

Sexy Forms for Merb matt

July 4th, 2008

I've written a little lib which piggy backs on the standard merb form helpers to give you nicer looking forms with help from Andy and Martyn.

We weren't happy with our forms and we were writing too much code. So we wrapped some goodies around the merb form helpers. We've got notes, inline errors, required / not required formatting, and cancel buttons. The API is so minimal you won't be able to resist.

Here's an example of the ruby code, the generated HTML, and what it looks like with some sexy CSS:

Example Code

Ruby

<%= field(:text, :nickname, :required => true, :note => "You'll be known by this on the site. Don't use your real name. It has to be unique, and you can't change it later.")%>
<%= field(:password, :password, :required => true)%>
<%= field(:password, :password_confirmation, :required => true, :note => "Type the same password again, just to make sure we've got it right" )%>
<%= field(:checkbox, :terms_and_conditions, :required => true, :value => true, :label=>"I accept the terms and conditions")%>
<%= form_submit "Sign Up!" %>

Html Generated

<div class="field password required">
<label for="user_password">Password</label>
<input type="password" class="password" name="user[password]" id="user_password" />
</div>

<div class="field password required">
<label for="user_password_confirmation">Password confirmation</label>
<input type="password" class="password" name="user[password_confirmation]" id=
"user_password_confirmation" />

<p class="note">Type the same password again, just to make sure we've got it right
<abbr title="smile">: )</abbr></p>
</div>

<div class="field checkbox required">
<input type="hidden" class="hidden" name="user[terms_and_conditions]" value="0" />
<input type="checkbox" class="checkbox" name="user[terms_and_conditions]" value="1"
id="user_terms_and_conditions" />
<label for="user_terms_and_conditions">I accept the terms and conditions</label>
</div>

<div class="field controls">
<button type="submit" class="positive">Sign Up!</button>
</div>

Html with Errors

<div class="field error password required">
<label for="user_password">Password</label>
<input type="password" class="error password" name="user[password]" id="user_password" />

<p class="error">can't be blank and is too short (minimum is 4 characters)</p>
</div>

<div class="field error password required">
<label for="user_password_confirmation">Password confirmation</label>
<input type="password" class="error password" name="user[password_confirmation]" id="user_password_confirmation" />

<p class="note">Type the same password again, just to make sure we've got it right
<abbr title="smile">: )</abbr></p>

<p class="error">can't be blank</p>
</div>

<div class="field error checkbox required">
<input type="hidden" class="hidden" name="user[terms_and_conditions]" value="0" />
<input type="checkbox" class="error checkbox" name="user[terms_and_conditions]" value="1" id="user_terms_and_conditions" />
<label for="user_terms_and_conditions">I accept the terms and conditions</label>

<p class="error">must be accepted</p>
</div>

Sexy Form

sexyform

You can be the judge of the sexyness of these forms, but they are much nicer than the default error message in my opinion.

Code/CSS

Code is licensed under the MIT License, and under no guarantee that it will not break if they change the external API for form helpers.

Install

Add the file in your lib folder, add the dependency in your init.rb and then include in your global helpers.

Enjoy!

8 Responses to “Sexy Forms for Merb”

  • Cristi Balan

    Nice html output. It’s exactly what I use for forms most of the time.

    ps: Your code is experiencing random smilies and missing textfields :)

  • Thomas Hurst

    Be warned, having the label to the side instead of above (like on this comment form) can be bad for accuracy and eye movement. With it to the side people zig-zag, with them above they can take the form in with a single scan and are less likely to need to re-check labels or mix up inputs.

    A nice small study on it: http://www.uxmatters.com/MT/archives/000107.php

  • Matt

    @thomas, the position of the labels is set by css so you can set it where you like, just override it.

  • Gregg Pollack

    For Rails there’s the Awesome Fields plugin which we talked about on a previous podcast.

    http://github.com/Shadowfiend/awesome_fields/tree/master

    Similar idea.

  • Xavier Shay

    Ah, but if you want sexy forms that are also accessible, you want formtastic (rails): http://github.com/justinfrench/formtastic/tree/master

  • Kyle Drake

    I like this approach, and I think this is the way to go. I would much rather have validations on the client side (or at least have it as an option), even if I do use validations on the models for safety and to stop people from mucking with things. Doing validations on submit blows up file uploads too, which is thoroughly annoying.

    I look forward to when Merb gets a little more stable and I’ll definitely use this code or something closely resembling it. Thanks!

  • MJ

    Nice form output!

    We’re just starting to experiment with Merb and it’s good to see there are other dev-shops out there embracing this newer technology.

  • uysys

    http://www.uysys.com

Sorry, comments are closed for this article.