GithubHelp home page GithubHelp logo

redpist / html5_validators Goto Github PK

View Code? Open in Web Editor NEW

This project forked from amatsuda/html5_validators

0.0 4.0 0.0 139 KB

A gem/plugin for Rails 3 & Rails 4 that enables client-side validation using ActiveModel + HTML5 Form Validation

License: MIT License

Ruby 100.00%

html5_validators's Introduction

HTML5Validators

Automatic client-side validation using HTML5 Form Validation

What is this?

html5_validators is a gem/plugin for Rails 3 that enables 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 => requried

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

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

  • And more (coming soon…?)

Disabling automatic client-side validation

There are three ways to cancel the automatic HTML5 validation

  • form_for option

Set ‘auto_html5_validation: false` to `form_for` parameter

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

Write ‘auto_html5_validation = false` in ActiveModelish class

Model:
  class User < ActiveRecord::Base
    self.auto_html5_validation = false
  end

Controller:
  @user = User.new

View:
  <%= form_for @user do |f| %>
    ...
  <% end %>

Supported versions

  • Ruby 1.8.7, 1.9.2, 1.9.3, 2.0, 2.1 (trunk)

  • Rails 3.0.x, 3.1.x, 3.2.x, 4.0.x, 4.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 © 2011 Akira Matsuda. See MIT-LICENSE for further details.

html5_validators's People

Contributors

amatsuda avatar redpist avatar ursm avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.