百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
H

html5_validators

> 前端框架
开源

用于铁路3、铁路4、铁路5和铁路6的宝石/插件,可以使用ActiveModel + HTML5形式验证客户端

302 stars0 点赞4 次浏览
访问官网GitHub

工具介绍

用于铁路3、铁路4、铁路5和铁路6的宝石/插件,可以使用ActiveModel + HTML5形式验证客户端

HTML5Validators

Automatic client-side validation using HTML5 Form Validation

What is this?

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.

Features

PresenceValidator => required

  • Model
ruby
class User
  include ActiveModel::Validations
  validates_presence_of :name
end
  • View
erb

other text_fieldish helpers, text_area, radio_button, and check_box are also available

  • HTML
xml
  • SPEC

http://dev.w3.org/html5/spec/Overview.html#attr-input-required

LengthValidator => maxlength

  • Model
ruby
class User
  include ActiveModel::Validations
  validates_length_of :name, maximum: 10
end
  • View
erb

text_area is also available

  • HTML
xml
  • SPEC

http://dev.w3.org/html5/spec/Overview.html#attr-input-maxlength

NumericalityValidator => max, min

  • Model
ruby
class User
  include ActiveModel::Validations
  validates_numericality_of :age, greater_than_or_equal_to: 20
end
  • View (be sure to use number_field)
erb
  • HTML
xml
  • SPEC

http://dev.w3.org/html5/spec/Overview.html#attr-input-max http://dev.w3.org/html5/spec/Overview.html#attr-input-min

And more (coming soon...?)

:construction:

Disabling automatic client-side validation

There are four ways to cancel the automatic HTML5 validation.

1. Per form (via form_for option)

Set auto_html5_validation: false to form_for parameter.

  • View
erb

  ...

2. Per model instance (via model attribute)

Set auto_html5_validation = false attribute to ActiveModelish object.

  • Controller
ruby
@user = User.new auto_html5_validation: false
  • View
erb

  ...

3. Per model class (via model class attribute)

Set auto_html5_validation = false to ActiveModelish class' class variable. This configuration will never be propagated to inherited children classes.

  • Model
ruby
class User 
  ...

4. Globally (via HTML5Validators module configuration)

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.

  • Controller
ruby
# 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

Supported versions

  • Ruby 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2, 3.3 (trunk)

  • Rails 3.2.x, 4.0.x, 4.1, 4.2, 5.0, 5.1, 5.2, 6.0, 6.1, 7.0, 7.1 (edge)

  • HTML5 compatible browsers

Installation

Put this line into your Gemfile:

ruby
gem 'html5_validators'

Then bundle:

% bundle

Notes

When accessed by an HTML5 incompatible legacy browser, these extra attributes will just be ignored.

Todo

  • more validations

Copyright

Copyright (c) 2011 Akira Matsuda. See MIT-LICENSE for further details.

Issues· 13 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyactivemodelactiverecordhtml5rails

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架