rails/rails — pull request #3590 by amatsuda (2011-11-10 06:06:43+00:00)
Title: automatic client side validation with HTML 5 "required" attribute

This patch was actually written more than a year ago, but unfortunately had lost somewhere between [Lighthouse](https://rails.lighthouseapp.com/projects/8994/tickets/5120) and GitHub. So let me propose here again...

HTML5 has a new feature called [Client-side form validation](http://www.w3.org/TR/html5/forms.html#client-side-form-validation) and ActiveModel::Validators lets us easily know what kind of validations are defined on our models.

So, I came up with another new feature combining these two technologies.

Now, when creating a form with a model which has a `validates_presence_of` field,

```ruby
class User < ActiveRecord::Base
validates_presence_of :name
end
```

```erb
<%= form_for User.new do |f| %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
```

the `required="required"` attribute are automagically injected into the generated input field.

```html

```

This enables seamless client-side validation without any extra JavaScript coding.

I baked this feature into a gem for Rails 3, so please take a look at https://github.com/amatsuda/html5_validators for more documentations, and give it a try to see how it actually works.