amatsuda / html5_validators Goto Github PK
View Code? Open in Web Editor NEWA gem/plugin for Rails 3, Rails 4, Rails 5, and Rails 6 that enables client-side validation using ActiveModel + HTML5 Form Validation
License: MIT License
A gem/plugin for Rails 3, Rails 4, Rails 5, and Rails 6 that enables client-side validation using ActiveModel + HTML5 Form Validation
License: MIT License
The class method auto_html5_validation
is added to ActiveRecord::Base, when imho it should be in ActiveModel::Model
. This would allow auto_html5_validation to work with objects that just include ActiveModel::Model
. (Which is often done for custom form objects, etc.)
validates :country_code, presence: true
translates to
<select name="user[country_code]">
instead of
<select required="required" name="user[country_code]">
Hi!
I've noticed that
validates :first_name, length: { minimum: 2, maximum: 50 }
translates to
<input maxlength="50" minlength="2" size="50" type="text">
<input required="required" maxlength="50" minlength="2" size="50" type="text">
If you agree, I could prepare a pull request with that, are you interested?
The following Rails code:
# User model
validates :state, inclusion: { in: ['unconfirmed', 'confirmed', 'locked', 'deleted', 'subaccount'] }
# user edit view
= f.text_field :state
can be translated into the following html code:
<input id="user_state" name="user[state]" type="text" pattern="(unconfirmed)|(confirmed)|(locked)|(deleted)|(subaccount)"/>
Can I send a PR?
Related to #23
I'm using Mongoid as my object mapper. So since I don't require the ActiveRecord railtie in my app the ActiveRecord on_load hook doesn't run and the ActiveModel helper methods don't get loaded in.
Any chance the loading code could be changed to work with any ActiveModel based mapper?
It would be nice if, instead of a single field failing validation being highlighted, the user fixes that field, and then attempts to submit again and they get the next field that failed, that all fields that fail validation are highlighted at the same time. This will provide a better user experience and less steps to get a proper input from the user.
I'm using auto_html5_validation: false
option for form_for
tag with model object namespace.
ex:
<%= form_for([:admin, @client], auto_html5_validation: false) do |f| %>
so, it doesn't work.
because
variables named 'record' is Array...
in html5_validators's form_helpers.rb L:22
From my spider test:
[979faece-c1e5-4d78-ac39-0b73b12863c4] [299:68.43] ActionView::Template::Error (undefined method `auto_html5_validation' for Patchinfo:Class):
[979faece-c1e5-4d78-ac39-0b73b12863c4] [299:68.43] 11: %strong
[979faece-c1e5-4d78-ac39-0b73b12863c4] 12: = f.label(:packager, 'Packager:')
[979faece-c1e5-4d78-ac39-0b73b12863c4] 13: %br/
[979faece-c1e5-4d78-ac39-0b73b12863c4] 14: = f.text_field :packager, required: true
[979faece-c1e5-4d78-ac39-0b73b12863c4] 15: :javascript
[979faece-c1e5-4d78-ac39-0b73b12863c4] 16: $("#packager").autocomplete({source: '#{url_for controller: 'user', action: 'autocomplete'}', search: function(event, ui) {
[979faece-c1e5-4d78-ac39-0b73b12863c4] 17: $(this).addClass('loading-spinner');
[979faece-c1e5-4d78-ac39-0b73b12863c4] [299:68.43]
[979faece-c1e5-4d78-ac39-0b73b12863c4] [299:68.43] app/views/webui/patchinfo/_form.html.haml:14:in `block in _app_views_webui_patchinfo__form_html_haml__1383717022713791384_47354342968160'
[979faece-c1e5-4d78-ac39-0b73b12863c4] app/views/webui/patchinfo/_form.html.haml:2:in `_app_views_webui_patchinfo__form_html_haml__1383717022713791384_47354342968160'
[979faece-c1e5-4d78-ac39-0b73b12863c4] app/views/webui/patchinfo/edit.html.haml:3:in `_app_views_webui_patchinfo_edit_html_haml__1907723402339578054_47354343006520'
[979faece-c1e5-4d78-ac39-0b73b12863c4] config/initializers/wrap_parameters.rb:38:in `call'
[979faece-c1e5-4d78-ac39-0b73b12863c4] test/functional/webui/spider_test.rb:99:in `crawl'
[979faece-c1e5-4d78-ac39-0b73b12863c4] test/functional/webui/spider_test.rb:161:in `test_spider_as_admin'
[979faece-c1e5-4d78-ac39-0b73b12863c4] test/test_helper.rb:118:in `block in __run'
[979faece-c1e5-4d78-ac39-0b73b12863c4] test/test_helper.rb:118:in `map'
[979faece-c1e5-4d78-ac39-0b73b12863c4] test/test_helper.rb:118:in `__run'
It only fails for Active Model, not for Active Record.
Hi @amatsuda and thanks for great gem!
I want to know do you have any plans to support Regexp in a future? Seems like this library really helps to avoid duplication between client and server, but with regexp I have to copy them anyway.
Or, maybe you have any advice for that?
I have for example, validation in backend with
value =~ /\A(4|9)\d{7}\Z/
and for frontend:
<%= f.telephone_field(:phone,
placeholder: 'Mobile',
pattern: '^[49]\d{7}$') %>
I get:
undefined method `auto_html5_validation' for #Class:0x007f11bd92f6a0
My guess is that you require some javascript file to be added to the manifest when not using //= require_tree .
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.