davyjoneslocker / client_side_validations Goto Github PK
View Code? Open in Web Editor NEWClient Side Validations made easy for Ruby on Rails
License: MIT License
Client Side Validations made easy for Ruby on Rails
License: MIT License
Not a single one of my selects are validated for required.
Code usually looks something like this:
=f.inputs name: locals[:name] do
=f.input :price, label:'Price/Cost', input_html: { maxlength:10, size: 10 }
=f.input :currency, as: :select, collection: major_currencies(Money::Currency::TABLE)
=f.input :type, as: :select, collection: ['Cost', 'Price']
Let's say currency is a required feel it doesn't matter because it don't get validated. Do I need to select nothing for the validation to take? I usually have "Please select something" as the preselected value and my guess is that that is what is causing problems.
I've developed using Chrome. Safari has several errors, Firefox as well. I need to test against all browsers before releasing the next RC
Is it possible to turn off validation for nested fields/associations in an easy manner? I try with the following below but preferably I'd like the possibility to turn it off for the whole darn thing inside the f.semantic_fields_for :address, validate: false do
=f.semantic_fields_for :delivery_address do |address|
=address.inputs name:"Delivery Address" do
=address.input :street_one, validate: false
=address.input :street_two, validate: false
=address.input :city, validate: false
=address.input :zip, validate: false
=address.input :region
=address.input :country, validate: false
=address.input :type, as: :hidden
=address.input :addressable_type, as: :hidden
=address.input :addressable_id, as: :hidden
Is something like that already implemented or is it behind the monitor again? I wouldn't mind supplying a patch if I just knew where to start hacking :)
The uniqueness validator middlware is pretty limited. It needs to be improved to work just like ActiveRecord's
Hi Brian!
as the subject says:
def validate_each(record, attr_name, value)
without the 's' in the EmailValidator class works for me. There might be a typo in here:
http://api.rubyonrails.org/classes/ActiveModel/EachValidator.html
--Frank
Hi,
I uncommented the code in the client_side initializer but for required fields it is always using the formtastic error message instead of the client side error. Even though I turned off generation of error messages with formtastic to let client_side generate that for me. For my email validator it works fine if I write something in the field but if I leave it empty I get the ugly formtastic message with
Not sure if the issue is infront of the screen or with formtastic or with client_side_validations but when my validations pass the error message don't disappear. I am probably doing something wrong but I can't figure out what.
If I miss to enter a name that is required I get a message saying: "can't be blank" but when I enter something in that textbox the message don't go away. Is this something I am supposed to code myself?
Not really an issue, just heads up - I'm using this gem on Windows Ruby 1.9.2p180 Rails 3.0.5, developing an app that must support IE6 ( 6.0.2900.5512 ) & IE 7 ( which I noticed you haven't tested under) - so far no problems.
All default validations & custom validations ( like url and email format work fine), under IE 6, 7 & 8.
Thank you very much for a great gem.
Currently ClientSideValidations assumes the :error message to be the last in the stack. However, SimpleForm allows this order to be overridden. The order should be respected.
Hi,
I am using devise gem for authentication and for signup, but this gem is not showing the error messages for the fields whose validations are written inside devise gem.
E.g validates_presence_of :email is not working initially but if write the same in user model it shows the error message. Do i need to override all the validations in user model ?? or is there any other work around for this ?
Hi Brian!
When I fire a
http://127.0.0.1:3000/validators/uniqueness.json?case_sensitive=true&scope=&user[email]=user10%40mail.com
I get an
undefined method `each' for "":String
error here:
lib/client_side_validations/mongoid/middleware.rb:13:in `is_unique?'
That's this line:
(params[:scope] || {}).each do |key, value|
Could it be, that in this case params[:scope] is not a Hash but a string and ruby1.9.2 bails out?
--Frank
ClientSideValidations needs to be required before SimpleForm and Formtastic in order to work properly. This is pretty annoying and shouldn't be the case.
The '=' operator is not case sensitive in MySQL. This is solved in Arel/master and Rails 3.1/master
Paperclip uses this, it should be ignored
Currently breaks with single item scope on uniqueness
Validators (especially remote validators) don't need to run if the value in the input hasn't changed.
There seem to be several issues before this will work.
Hi, Brian.
Been playing with the new releases and everything seems to be kosher . (By the way - no issues under FF 4 so far) however I'm back with a new idea :-)
I guess this is a feature request. Sometimes there are situations where user input doesn't necessarily pass part of your validation but can still be legitimate input. You want to be able to let the form be able to be submitted but let a user know that it is potentially invalid or mistyped input, so they check it twice.
This is from a real life app I worked on a while back, and it can serve as a great example. There was a vehicle parts order table, where users were required to provide a VIN number of the vehicle. Unfortunately the VIN db that we used to verify the VIN numbers would only hold 3 years worth of VIN numbers. We had to verify against that external service (customer requirement), however if the vehicle was older than 3 years it was mostly like not in the DB, while still having a legitimate VIN number. So in that case we would verify requirements like "17 alphanumeric characters long" but since we couldn't verify the actual existence of a VIN, we would use some javascript voodoo to pop a warning for them on field blur to make sure they verify the number (actually a little yellow div that asked them politely to verify the number was typed in correctly since it wasn't in our records)
Another example is if you want users to provide full legal names, but most people tend to submit things like Mike vs Michael, and you want to emphasize it to them that it's valid input but you want them to verify that it's their legal name. Sort of a fuzzy validation (tm) :-)
I'm sure there are other situations where it would be useful to let through a form that passes some validation but fails some more obscure ones with a warning. Or allow validations that would be "nice to pass". What do you think about it?
One more thing - what if your validation depends on external service that happens to be down. You may still want to be able to process the form even if it fails validating against that service.
Once again thanks for a great gem.
Nick
I have the following validator:
validates :price
:numericality => { :greater_than => 0 },
:unless => Proc.new { |item| xxxxxx }
I removed the proc body for simplicity. This still generates client side validation, however. I assume that should not be the case based on documentation.
For length
, confirmation
and uniqueness
And also follow naming conventions
There should be callbacks on the validators. I propose the following:
element:validate:before
element:validate:after
element:validate:pass
element:validate:fail
form:validate:before
form:validate:after
form:validate:pass
form:validate:fail
Hello.
I have validations that are done in the following way:
validates :email, :presence => true, :length => {:minimum => 5, :maximum => 50}, :email_format => true
I also have a custom validator
class EmailFormatValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value)
unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
object.errors[attribute] << (options[:message] || "is not formatted properly. Try something like: [email protected] ")
end
end
end
Client_side_validations - validates my field for minimum / maximum , but not :email_format , which is just handled by server-side validation on submit.
Is this intended behavior or am I missing something? I saw the custom validations wiki page, just wondering if this setup ( like in my example) is going to be supported - this would make the usage of validates_email method ( as in your example on wiki page) unnecessary it seems.
Hi,
Formtastic offers a way at the end of it's initializer file to easily extend it's form builder, in my case, I wanted to add a few class along the way in some cases, but the thing is that the builder is MyApp::FormBuilder, which is not recognized by the javascript as a builder. Right now, I added :
def self.client_side_form_settings(options, form_helper)
{
:type => self.superclass.to_s,
:inline_error_class => ::Formtastic::SemanticFormBuilder.default_inline_error_class
}
end
to my builder so that it gets the right class name. But it's now something else that I have to look for when upgrading...
I don't really understand why it's "super.to_s" in the first case and not "Formtastic::SemanticFormBuilder" right away as it's already written verbatim on the following line :-)
Some of the async form submission tests are failing in Firefox and IE. Kind of annoying
It would be nice to ignore individual validations on each form input:
= simple_form_for @user do |user|
= user.input :email, :validate => { :ignore => [:uniqueness] }
I don't think the confirm password message is so useful. It should appear on the confirm field, not on the password field.
On my model, I have a custom validates_with validator.
With this in place, I get a TypeError with the message, "nil is not a symbol". If I remove the validator from the model, it appears to work fine.
Thanks,
Scott
Issue with confirming uniqueness when id is included
I have a model with a float
attribute and when I turn csv on for its form I get the following type error:
can't iterate from Float
Removing this field from the form makes everything work as expected.
I'm on Rails 3.0.5 using ActiveRecord, form_for
and csv gem 3.0.0.beta.7.
Since it's a minor typo I didn't feel like forking & pull request:
rails g client_side_validations:instlal should be rails g client_side_validations:install
Using response status codes is better than parsing 'true' and 'false'
Once the Rails 3 rewrite is released support should be added
Having to run the install generator every time is pretty annoying. When Rails 3.1 drops Client Side Validations should take advantage of the new Engines to serve the asset from the gem instead of copying it into public/javascript
If I omit :validate => true from a fields_for call, a NoMethodError is thrown:
= fields_for @wine.wine_region do |wine_region_form|
However adding it does not throw an exception:
= fields_for @wine.wine_region, :validate => true do |wine_region_form|
However, no validation for nested properties is generated.
I'm using rails 3.0.5, haml and simple form. Does it work with haml?
Brian, here is a little issue I discovered. It has more to do with user experience.
You know how when you are filling in a form for common fields (name, email, address), most browsers provide little drop down with history of what you filled in before? Well if you tried to fill a line, and it failed validation after you tabbed away from it and then came back to it, that dropdown will try to help you again. But [data-failed-once] keyup event keeps refreshing the field trying to validate it, as you type. As a result it keeps flickering which is very annoying, plus it's not letting user to use the browser dropdown.
Also trying to force the dropdown on keydown, also doesn't work. Although - that seems to be an issue in firefox, not chrome.
I see why would you want to validate as you type, but then it creates unpleasant experience, especially for people who are used to reusing value from that dropdown. I think the expected behavior would be to validate on blur, maybe? What do you think? Or is there a way to disable the dropdown altogether, so at least it's not confusing?
I'm not sure if it qualifies as bug, rather than a unexpected feature :-)
My form has a text field and a textarea next to each other.
<%= form.label :duration, "Duration (hours)", :class => "required" %>
<%= form.text_field :duration, :autofocus => true %>
<%= form.label :description, "Description", :class => "required" %>
<%= form.text_area :description %>
Both are required and the textarea has an additional uniqueness test on it.
When I leave the text field empty and click on the textarea it invalidates both of them. I never get the textarea focused and have to click on it again.
This same effect affects the submit button. When I fulfill both the text field's and textarea's validation requirements and click on the submit button it does not submit the form. Instead, it validates the once-invalid fields and I have to click submit a second time to actual submit the form.
I realize this description might be lacking in clarity. I can shoot a quick screencast if it would help.
You're firing validation on the "focusout" event. This problem could be related to when that event actually triggers. Let me know if I can troubleshoot in any way.
I'm on latest master as of: d6db7fc
I don't use ActiveRecord in my project, but I use ActiveModel (Validations) and had problem with gem. In /lib/client_side_validations.rb you should check if ActiveModel is defined if ActiveRecord is not defined, i.e.:
if defined?(::ActiveRecord)
require 'client_side_validations/active_record'
elsif defined?(::ActiveModel)
require 'client_side_validations/active_model'
end
Hi! I hav some trouble with this. When I enter invalid data - class of div changed to field_with_errors, but does not appear any error messages. Why can this be? Rails 3.0.4
Hi,
i m getting this error with mongoid
gem 'rails'
gem 'haml-rails'
gem 'compass', '>= 0.10.6'
gem 'jquery-rails', '>=0.2.6'
gem 'formtastic'
gem 'client_side_validations', '~> 3.0.0'
gem 'mongoid'
gem 'bson_ext'
gem 'kaminari'
gem "bcrypt-ruby", :require => "bcrypt"
gem "cancan"
my form:
= semantic_form_for @user, :validate => true do |f|
=f.inputs do
trace:
mongoid (2.0.1) lib/mongoid/attributes.rb:159:in method_missing' client_side_validations (3.0.4) lib/client_side_validations/action_view/form_builder.rb:81:in
apply_client_side_validators'
(eval):2:in text_field_with_client_side_validations' formtastic (1.2.3) lib/formtastic.rb:655:in
basic_input_helper'
formtastic (1.2.3) lib/formtastic.rb:660:in string_input' formtastic (1.2.3) lib/formtastic.rb:1280:in
inline_input_for'
formtastic (1.2.3) lib/formtastic.rb:132:in block in input' formtastic (1.2.3) lib/formtastic.rb:131:in
map'
formtastic (1.2.3) lib/formtastic.rb:131:in `input'
i did the installation as said in the wiki
initializer got created
any idea?
best regards
I'm not sure exactly how to pinpoint exactly what's wrong here, but I gather that it's because I have a nested form.
My models are:
Company has_many :employees
Employee belongs_to :company
I have a nested form for my company#new
action that also creates the first employee. I'm using f.fields_for :employees
do accomplish this.
Here's what I'm seeing:
If this is a bug, happy to contribute a fix. Just need a point in the right direction.
Hello.
There are few items on the forms, that are optional, but if provided should be validated.
i.e.: I have a url in a contact form, that should be validated as valid ( pass regex (using custom validation) & maximum limitations), but if not provided, the field shouldn't be validated.
Works fine on server-side validations, but not client side. Keeps checking the field, no matter what I do. I even tried to set :presence => :false
explicitly and still it keeps checking the field. this seems to be an issue only on fields that use custom validation. I have a field called company, that only validate :company, :length => {:maximum => 50}
and this one seems ok.
here is my regex: /[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/i
Unless I missed setting up some option - that seems like a small bug to me.
After an error all events from my input are missing.
As i took a quick look at the code a lot of replaces is going on and i think somewhere all events are gone. I wasnt abble to fix it so i am opening this issue.
Another thing that i did noticed is that if you try to submit the form you get an error for the first time, but if you try again, the form is submitted.
The Mongoid Uniqueness validator is pretty limited. This won't be a block for a 3.0 release. Should be done for a 3.0 patch release.
Potential problem with selects again. Some of my selects are not cleared even after selecting a value. See https://gist.github.com/949020 for more information. The haml file is what I use and the html file is what is generated. For frame_id and color_id the error refuses to go away regardless what I select. For lens_tint_id the error goes away when I select something except the "" blank option.
I am a little unsure about where the problem is exactly. If I fill in all required fields in that form I still can't post it back to the server but if I turn client side validations off then server side validation says it is ok. What more information can I provide?
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.