All Projects → amatsuda → Html5_validators

amatsuda / Html5_validators

Licence: mit
A gem/plugin for Rails 3, Rails 4, Rails 5, and Rails 6 that enables client-side validation using ActiveModel + HTML5 Form Validation

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Html5 validators

Ooor
Odoo Ruby JSON client. Emulates ActiveRecord enough (as much as Mongoid; Implements ActiveModel) to make Rails development with an Odoo datastore straightforward
Stars: ✭ 184 (-39.07%)
Mutual labels:  activerecord, rails
Seamless database pool
Add support for master/slave database clusters in ActiveRecord to improve performance.
Stars: ✭ 222 (-26.49%)
Mutual labels:  activerecord, rails
Jsonapi Utils
Build JSON API-compliant APIs on Rails with no (or less) learning curve.
Stars: ✭ 191 (-36.75%)
Mutual labels:  activerecord, rails
Torque Postgresql
Add support to complex resources of PostgreSQL, like data types, array associations, and auxiliary statements (CTE)
Stars: ✭ 130 (-56.95%)
Mutual labels:  activerecord, rails
Public activity
Easy activity tracking for models - similar to Github's Public Activity
Stars: ✭ 2,822 (+834.44%)
Mutual labels:  activerecord, rails
Graphql Query Resolver
Minimize N+1 queries generated by GraphQL and ActiveRecord
Stars: ✭ 148 (-50.99%)
Mutual labels:  activerecord, rails
Secondbase
Seamless second database integration for Rails.
Stars: ✭ 216 (-28.48%)
Mutual labels:  activerecord, rails
Where Or
Where or function backport from Rails 5 for Rails 4.2
Stars: ✭ 116 (-61.59%)
Mutual labels:  activerecord, rails
Clowne
A flexible gem for cloning models
Stars: ✭ 260 (-13.91%)
Mutual labels:  activerecord, rails
Scenic
Scenic is maintained by Derek Prior, Caleb Hearth, and you, our contributors.
Stars: ✭ 2,856 (+845.7%)
Mutual labels:  activerecord, rails
Activerecord where assoc
Make ActiveRecord do conditions on your associations
Stars: ✭ 126 (-58.28%)
Mutual labels:  activerecord, rails
Pluck to hash
Extend ActiveRecord pluck to return array of hashes
Stars: ✭ 275 (-8.94%)
Mutual labels:  activerecord, rails
Closure tree
Easily and efficiently make your ActiveRecord models support hierarchies
Stars: ✭ 1,665 (+451.32%)
Mutual labels:  activerecord, rails
Pg party
ActiveRecord PostgreSQL Partitioning
Stars: ✭ 294 (-2.65%)
Mutual labels:  activerecord, rails
Calculate All
calculate_all method for aggregate functions in Active Record
Stars: ✭ 118 (-60.93%)
Mutual labels:  activerecord, rails
Activerecord Turntable
ActiveRecord Sharding Plugin
Stars: ✭ 206 (-31.79%)
Mutual labels:  activerecord, rails
Active record Events
Manage timestamps in ActiveRecord models
Stars: ✭ 109 (-63.91%)
Mutual labels:  activerecord, rails
Active hash relation
ActiveHash Relation: Simple gem that allows you to run multiple ActiveRecord::Relation using hash. Perfect for APIs.
Stars: ✭ 115 (-61.92%)
Mutual labels:  activerecord, rails
Activerecord Postgres enum
Integrate PostgreSQL's enum data type into ActiveRecord's schema and migrations.
Stars: ✭ 227 (-24.83%)
Mutual labels:  activerecord, rails
Elasticsearch Rails
Elasticsearch integrations for ActiveModel/Record and Ruby on Rails
Stars: ✭ 2,896 (+858.94%)
Mutual labels:  activerecord, rails

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
class User
  include ActiveModel::Validations
  validates_presence_of :name
end
  • View
<%= f.text_field :name %>

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

  • HTML
<input id="user_name" name="user[name]" required="required" type="text" />
  • SPEC

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

PresenceValidator

LengthValidator => maxlength

  • Model
class User
  include ActiveModel::Validations
  validates_length_of :name, maximum: 10
end
  • View
<%= f.text_field :name %>

text_area is also available

  • HTML
<input id="user_name" maxlength="10" name="user[name]" size="10" type="text" />
  • SPEC

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

NumericalityValidator => max, min

  • Model
class User
  include ActiveModel::Validations
  validates_numericality_of :age, greater_than_or_equal_to: 20
end
  • View (be sure to use number_field)
<%= f.number_field :age %>
  • HTML
<input id="user_age" min="20" name="user[age]" size="30" type="number" />
  • SPEC

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

NumericalityValidator

And more (coming soon...?)

🚧

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
<%= form_for @user, auto_html5_validation: false do |f| %>
  ...
<% end %>

2. Per model instance (via model attribute)

Set auto_html5_validation = false attribute to ActiveModelish object.

  • Controller
@user = User.new auto_html5_validation: false
  • View
<%= form_for @user do |f| %>
  ...
<% end %>

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
class User < ActiveRecord::Base
  self.auto_html5_validation = false
end
  • Controller
@user = User.new
  • View
<%= form_for @user do |f| %>
  ...
<% end %>

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
# 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.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7 (trunk)

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

  • HTML5 compatible browsers

Installation

Put this line into your Gemfile:

gem 'html5_validators'

Then bundle:

% bundle

Notes

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

Todo

  • more validations

Copyright

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].