New Bamboo Web Development

Bamboo blog. Our thoughts on web technology.

Sexy Forms for Merb

by matt

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

1 <%= 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.")%>
2 <%= field(:password, :password, :required => true)%>
3 <%= field(:password, :password_confirmation, :required => true, :note => "Type the same password again, just to make sure we've got it right" )%>
4 <%= field(:checkbox, :terms_and_conditions, :required => true, :value => true, :label=>"I accept the terms and conditions")%>
5 <%= form_submit "Sign Up!" %>

Html Generated

 1 <div class="field password required">
 2 <label for="user_password">Password</label>
 3 <input type="password" class="password" name="user[password]" id="user_password" />
 4 </div>
 5 
 6 <div class="field password required">
 7 <label for="user_password_confirmation">Password confirmation</label>
 8 <input type="password" class="password" name="user[password_confirmation]" id=
 9 "user_password_confirmation" />
10 
11 <p class="note">Type the same password again, just to make sure we've got it right
12 <abbr title="smile">: )</abbr></p>
13 </div>
14 
15 <div class="field checkbox required">
16 <input type="hidden" class="hidden" name="user[terms_and_conditions]" value="0" />
17 <input type="checkbox" class="checkbox" name="user[terms_and_conditions]" value="1"
18 id="user_terms_and_conditions" />
19 <label for="user_terms_and_conditions">I accept the terms and conditions</label>
20 </div>
21 
22 <div class="field controls">
23 <button type="submit" class="positive">Sign Up!</button>
24 </div>

Html with Errors

 1 <div class="field error password required">
 2 <label for="user_password">Password</label>
 3 <input type="password" class="error password" name="user[password]" id="user_password" />
 4 
 5 <p class="error">can't be blank and is too short (minimum is 4 characters)</p>
 6 </div>
 7 
 8 <div class="field error password required">
 9 <label for="user_password_confirmation">Password confirmation</label>
10 <input type="password" class="error password" name="user[password_confirmation]" id="user_password_confirmation" />
11 
12 <p class="note">Type the same password again, just to make sure we've got it right
13 <abbr title="smile">: )</abbr></p>
14 
15 <p class="error">can't be blank</p>
16 </div>
17 
18 <div class="field error checkbox required">
19 <input type="hidden" class="hidden" name="user[terms_and_conditions]" value="0" />
20 <input type="checkbox" class="error checkbox" name="user[terms_and_conditions]" value="1" id="user_terms_and_conditions" />
21 <label for="user_terms_and_conditions">I accept the terms and conditions</label>
22 
23 <p class="error">must be accepted</p>
24 </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!