jquery-validator 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +68 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/jquery-validator.gemspec +59 -0
- data/lib/generators/jquery_validator/install/jquery_validator_generator.rb +16 -0
- data/lib/generators/jquery_validator/install/templates/jquery_validator.js +19 -0
- data/lib/jquery-validator.rb +3 -0
- data/lib/jquery_validator/base.rb +95 -0
- data/lib/jquery_validator/form_builder.rb +30 -0
- data/spec/jquery-validator_spec.rb +7 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +94 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 mixtli
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
= jquery-validator
|
2
|
+
|
3
|
+
Rails plugin to generate jquery validations based on model validations. Use this plugin to keep your validations DRY.
|
4
|
+
This plugin requires Rails 3, Jquery, and the Jquery validation plugin.
|
5
|
+
|
6
|
+
Supported validations include:
|
7
|
+
* Presence
|
8
|
+
* Format
|
9
|
+
* Numericality
|
10
|
+
* Includes
|
11
|
+
* Excludes
|
12
|
+
* Length
|
13
|
+
* Confirmation
|
14
|
+
* Acceptance
|
15
|
+
|
16
|
+
== Usage
|
17
|
+
|
18
|
+
% rails generate jquery_validator:install
|
19
|
+
= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"
|
20
|
+
= javascript_include_tag "http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.js"
|
21
|
+
= javascript_include_tag "jquery_validator"
|
22
|
+
|
23
|
+
class User < ActiveRecord::Base
|
24
|
+
validate :login, :presence => true, :length => {:minimum => 3, :maximum => 10}
|
25
|
+
validate :email, :presence => true
|
26
|
+
validate :password, :presence => true, :confirmation => true, :length => {:minimum => 8}
|
27
|
+
validates_acceptance_of :eula
|
28
|
+
end
|
29
|
+
|
30
|
+
<%= form_for(@user) do |f| %>
|
31
|
+
<%=f.jquery_validations %>
|
32
|
+
Login: <%= f.text_field :login %><br/>
|
33
|
+
Password: <%= f.password_field :password %><br/>
|
34
|
+
Confirm: <%= f.password_field :password_confirmation %><br/>
|
35
|
+
Accept Terms <%= f.checkbox :eula %>
|
36
|
+
<%= f.submit %>
|
37
|
+
<% end %>
|
38
|
+
|
39
|
+
== Options
|
40
|
+
|
41
|
+
You can pass a :fields argument to jquery_validations to only validate certain fields. All other options are converted to JSON and passed directly to the jquery validate() method. You can pass javascript functions as strings. For example
|
42
|
+
|
43
|
+
<%= f.jquery_validators :fields => [:login, :email], :debug => true, :submitHandler => "function() { alert('Thank you!'); }" %>
|
44
|
+
|
45
|
+
If you want to be more unobtrusive, add
|
46
|
+
|
47
|
+
yield :javascripts
|
48
|
+
|
49
|
+
to the head section of your application.html.erb and surround your jquery_validators call with content_for like so:
|
50
|
+
|
51
|
+
<% content_for :javascripts do %>
|
52
|
+
<%= f.jquery_validators %>
|
53
|
+
<% end %>
|
54
|
+
|
55
|
+
|
56
|
+
== Note on Patches/Pull Requests
|
57
|
+
|
58
|
+
* Fork the project.
|
59
|
+
* Make your feature addition or bug fix.
|
60
|
+
* Add tests for it. This is important so I don't break it in a
|
61
|
+
future version unintentionally.
|
62
|
+
* Commit, do not mess with rakefile, version, or history.
|
63
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
64
|
+
* Send me a pull request. Bonus points for topic branches.
|
65
|
+
|
66
|
+
== Copyright
|
67
|
+
|
68
|
+
Copyright (c) 2010 mixtli. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "jquery-validator"
|
8
|
+
gem.summary = %Q{DRY up your rails validations with jquery}
|
9
|
+
gem.description = %Q{Uses the model validation logic you have already defined to generate javascript validation using jquery}
|
10
|
+
gem.email = "ronmcclain75@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/mixtli/jquery-validator"
|
12
|
+
gem.authors = ["mixtli"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
#require 'spec/rake/spectask'
|
22
|
+
#Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
# spec.libs << 'lib' << 'spec'
|
24
|
+
# spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
#end
|
26
|
+
|
27
|
+
#Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
# spec.libs << 'lib' << 'spec'
|
29
|
+
# spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
# spec.rcov = true
|
31
|
+
#end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "jquery-validator #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{jquery-validator}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["mixtli"]
|
12
|
+
s.date = %q{2010-06-09}
|
13
|
+
s.description = %q{Uses the model validation logic you have already defined to generate javascript validation using jquery}
|
14
|
+
s.email = %q{ronmcclain75@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"jquery-validator.gemspec",
|
27
|
+
"lib/generators/jquery_validator/install/jquery_validator_generator.rb",
|
28
|
+
"lib/generators/jquery_validator/install/templates/jquery_validator.js",
|
29
|
+
"lib/jquery-validator.rb",
|
30
|
+
"lib/jquery_validator/base.rb",
|
31
|
+
"lib/jquery_validator/form_builder.rb",
|
32
|
+
"spec/jquery-validator_spec.rb",
|
33
|
+
"spec/spec.opts",
|
34
|
+
"spec/spec_helper.rb"
|
35
|
+
]
|
36
|
+
s.homepage = %q{http://github.com/mixtli/jquery-validator}
|
37
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = %q{1.3.7}
|
40
|
+
s.summary = %q{DRY up your rails validations with jquery}
|
41
|
+
s.test_files = [
|
42
|
+
"spec/jquery-validator_spec.rb",
|
43
|
+
"spec/spec_helper.rb"
|
44
|
+
]
|
45
|
+
|
46
|
+
if s.respond_to? :specification_version then
|
47
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
48
|
+
s.specification_version = 3
|
49
|
+
|
50
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
51
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
54
|
+
end
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module JqueryValidator
|
2
|
+
class InstallGenerator < Rails::Generators::Base
|
3
|
+
desc 'Copies jquery_validator.js to public/javascripts'
|
4
|
+
def install_javascript
|
5
|
+
copy_file('jquery_validator.js', 'public/javascripts/jquery_validator.js')
|
6
|
+
end
|
7
|
+
def self.source_root
|
8
|
+
File.join(File.dirname(__FILE__), 'templates')
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.banner
|
12
|
+
"rails generate jquery_validator"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
jQuery.validator.addMethod("accepts", function(value, element) {
|
2
|
+
return $('input[name=' + element.name + '][type=checkbox]').attr('checked');
|
3
|
+
}, "You must accept the terms and conditions");
|
4
|
+
|
5
|
+
jQuery.validator.addMethod("excludes", function(value, element, params) {
|
6
|
+
return jQuery.inArray($(element).val(), params) == -1
|
7
|
+
}, "Invalid Input");
|
8
|
+
|
9
|
+
jQuery.validator.addMethod("includes", function(value, element, params) {
|
10
|
+
return jQuery.inArray($(element).val(), params) != -1
|
11
|
+
}, "Invalid Input");
|
12
|
+
|
13
|
+
jQuery.validator.addMethod("matches", function(value, element, params) {
|
14
|
+
return value.match(params);
|
15
|
+
}, "Invalid Input");
|
16
|
+
|
17
|
+
jQuery.validator.addMethod("nomatches", function(value, element, params) {
|
18
|
+
return !value.match(params);
|
19
|
+
}, "Invalid Input");
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'form_builder_extensions'
|
3
|
+
module JqueryValidator
|
4
|
+
class Base
|
5
|
+
def initialize(builder, validator)
|
6
|
+
@builder, @validator = builder, validator
|
7
|
+
end
|
8
|
+
|
9
|
+
def validate_arguments
|
10
|
+
{}
|
11
|
+
end
|
12
|
+
|
13
|
+
def attributes
|
14
|
+
@validator.attributes
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.factory(v, builder)
|
20
|
+
klass = v.class.to_s.split("::").last
|
21
|
+
klass = "JqueryValidator::#{klass}"
|
22
|
+
klass.constantize.new(builder, v)
|
23
|
+
rescue =>e
|
24
|
+
# No matching validator exists.
|
25
|
+
#Rails.logger.debug e.message
|
26
|
+
nil
|
27
|
+
end
|
28
|
+
|
29
|
+
class PresenceValidator < JqueryValidator::Base
|
30
|
+
def validate_arguments
|
31
|
+
{:rules => {"#{@builder.object_name}[#{@validator.attributes.first}]" => {'required' => true}}}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class AcceptanceValidator < JqueryValidator::Base
|
36
|
+
def validate_arguments
|
37
|
+
field = "#{@builder.object_name}[#{@validator.attributes.first}]"
|
38
|
+
{ :rules => { field => {:accepts => true}} }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class ConfirmationValidator < JqueryValidator::Base
|
43
|
+
def validate_arguments
|
44
|
+
field = "#{@builder.object_name}[#{@validator.attributes.first}_confirmation]"
|
45
|
+
{ :rules => { field => {:equalTo => "##{@builder.object_name}_#{@validator.attributes.first}"}} }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class ExclusionValidator < JqueryValidator::Base
|
50
|
+
def validate_arguments
|
51
|
+
field = "#{@builder.object_name}[#{@validator.attributes.first}]"
|
52
|
+
{ :rules => { field => {:excludes => @validator.options[:in] }}}
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class InclusionValidator < JqueryValidator::Base
|
57
|
+
def validate_arguments
|
58
|
+
field = "#{@builder.object_name}[#{@validator.attributes.first}]"
|
59
|
+
{ :rules => { field => {:includes => @validator.options[:in] }}}
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class LengthValidator < JqueryValidator::Base
|
64
|
+
def validate_arguments
|
65
|
+
field = "#{@builder.object_name}[#{@validator.attributes.first}]"
|
66
|
+
args = {:rules => {field => {}}}
|
67
|
+
args[:rules][field][:minlength] = @validator.options[:minimum] if @validator.options[:minimum]
|
68
|
+
args[:rules][field][:maxlength] = @validator.options[:maximum] if @validator.options[:maximum]
|
69
|
+
args
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class NumericalityValidator < JqueryValidator::Base
|
74
|
+
def validate_arguments
|
75
|
+
field = "#{@builder.object_name}[#{@validator.attributes.first}]"
|
76
|
+
args = {:rules => {field => {:number => true}}}
|
77
|
+
args[:rules][field][:min] = @validator.options[:greater_than_or_equal_to] if @validator.options[:greater_than_or_equal_to]
|
78
|
+
args[:rules][field][:max] = @validator.options[:less_than_or_equal_to] if @validator.options[:less_than_or_equal_to]
|
79
|
+
args[:rules][field][:digits] = true if @validator.options[:only_integer]
|
80
|
+
args
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
class FormatValidator < JqueryValidator::Base
|
85
|
+
def validate_arguments
|
86
|
+
field = "#{@builder.object_name}[#{@validator.attributes.first}]"
|
87
|
+
args = {:rules => {field => {}}}
|
88
|
+
args[:rules][field][:matches] = @validator.options[:with].source if @validator.options[:with]
|
89
|
+
args[:rules][field][:nomatches] = @validator.options[:without].source if @validator.options[:without]
|
90
|
+
args
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
module ActionView
|
3
|
+
module Helpers
|
4
|
+
class FormBuilder
|
5
|
+
def jquery_validators(options = {})
|
6
|
+
fields = options.delete(:fields)
|
7
|
+
|
8
|
+
Rails.logger.debug @object.class.validators.map(&:attributes)
|
9
|
+
@object.class.validators.each do |v|
|
10
|
+
Rails.logger.debug "doing validator #{v}"
|
11
|
+
if !fields || fields.include?(v.attributes.first)
|
12
|
+
if validator = JqueryValidator.factory(v, self)
|
13
|
+
options.deep_merge!(JqueryValidator.factory(v, self).validate_arguments)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
js = options.to_json
|
18
|
+
js.gsub!(/"\s*(function\(.*\})\s*\"/, '\1')
|
19
|
+
code = %Q[
|
20
|
+
<script>
|
21
|
+
$(document).ready(function() {
|
22
|
+
$("##{ActionController::RecordIdentifier.dom_id(@object, @object.new_record? ? :new : :edit)}").validate(#{js});
|
23
|
+
});
|
24
|
+
</script>
|
25
|
+
]
|
26
|
+
code.html_safe
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jquery-validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- mixtli
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-06-09 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 2
|
31
|
+
- 9
|
32
|
+
version: 1.2.9
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
description: Uses the model validation logic you have already defined to generate javascript validation using jquery
|
36
|
+
email: ronmcclain75@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- jquery-validator.gemspec
|
52
|
+
- lib/generators/jquery_validator/install/jquery_validator_generator.rb
|
53
|
+
- lib/generators/jquery_validator/install/templates/jquery_validator.js
|
54
|
+
- lib/jquery-validator.rb
|
55
|
+
- lib/jquery_validator/base.rb
|
56
|
+
- lib/jquery_validator/form_builder.rb
|
57
|
+
- spec/jquery-validator_spec.rb
|
58
|
+
- spec/spec.opts
|
59
|
+
- spec/spec_helper.rb
|
60
|
+
has_rdoc: true
|
61
|
+
homepage: http://github.com/mixtli/jquery-validator
|
62
|
+
licenses: []
|
63
|
+
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options:
|
66
|
+
- --charset=UTF-8
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
version: "0"
|
85
|
+
requirements: []
|
86
|
+
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.3.7
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: DRY up your rails validations with jquery
|
92
|
+
test_files:
|
93
|
+
- spec/jquery-validator_spec.rb
|
94
|
+
- spec/spec_helper.rb
|