
Security News
Insecure Agents Podcast: Certified Patches, Supply Chain Security, and AI Agents
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.
html5_validators
Advanced tools
Automatic client-side validation using HTML5 Form Validation
html5_validators is a gem/plugin for Rails 3+ that enables automatic client-side
validation using ActiveModel + HTML5. Once you bundle this gem on your app,
the gem will automatically translate your model validation code into HTML5
validation attributes on every form_for invocation unless you explicitly
cancel it.
class User
include ActiveModel::Validations
validates_presence_of :name
end
<%= f.text_field :name %>
other text_fieldish helpers, text_area, radio_button, and check_box are also available
<input id="user_name" name="user[name]" required="required" type="text" />
http://dev.w3.org/html5/spec/Overview.html#attr-input-required

class User
include ActiveModel::Validations
validates_length_of :name, maximum: 10
end
<%= f.text_field :name %>
text_area is also available
<input id="user_name" maxlength="10" name="user[name]" size="10" type="text" />
http://dev.w3.org/html5/spec/Overview.html#attr-input-maxlength
class User
include ActiveModel::Validations
validates_numericality_of :age, greater_than_or_equal_to: 20
end
<%= f.number_field :age %>
<input id="user_age" min="20" name="user[age]" size="30" type="number" />
http://dev.w3.org/html5/spec/Overview.html#attr-input-max http://dev.w3.org/html5/spec/Overview.html#attr-input-min

:construction:
There are four ways to cancel the automatic HTML5 validation.
Set auto_html5_validation: false to form_for parameter.
<%= form_for @user, auto_html5_validation: false do |f| %>
...
<% end %>
Set auto_html5_validation = false attribute to ActiveModelish object.
@user = User.new auto_html5_validation: false
<%= form_for @user do |f| %>
...
<% end %>
Set auto_html5_validation = false to ActiveModelish class' class variable.
This configuration will never be propagated to inherited children classes.
class User < ActiveRecord::Base
self.auto_html5_validation = false
end
@user = User.new
<%= form_for @user do |f| %>
...
<% end %>
Set config.enabled = false to Html5Validators module.
Maybe you want to put this in your test_helper, or add a controller filter as
follows for development mode.
# an example filter that disables the validator if the request has {h5v: 'disable'} params
around_action do |controller, block|
h5v_enabled_was = Html5Validators.enabled
Html5Validators.enabled = false if params[:h5v] == 'disable'
block.call
Html5Validators.enabled = h5v_enabled_was
end
Ruby 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6 (trunk)
Rails 3.2.x, 4.0.x, 4.1, 4.2, 5.0, 5.1, 5.2, 6.0 (edge)
HTML5 compatible browsers
Put this line into your Gemfile:
gem 'html5_validators'
Then bundle:
% bundle
When accessed by an HTML5 incompatible lagacy browser, these extra attributes will just be ignored.
Copyright (c) 2011 Akira Matsuda. See MIT-LICENSE for further details.
FAQs
Unknown package
We found that html5_validators demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.

Security News
Tailwind Labs laid off 75% of its engineering team after revenue dropped 80%, as LLMs redirect traffic away from documentation where developers discover paid products.

Security News
The planned feature introduces a review step before releases go live, following the Shai-Hulud attacks and a rocky migration off classic tokens that disrupted maintainer workflows.