client_side_validations 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -44,7 +44,7 @@ This will copy client_side_validations.js to "public/javascripts"
44
44
  Currently only [jquery.validate](http://bassistance.de/jquery-plugins/jquery-plugin-validation/) is supported so you will need to download [jQuery](http://docs.jquery.com/Downloading_jQuery) and the jQuery Validate plugin to "public/javascripts"
45
45
 
46
46
  ### Rack
47
- If you want to validate_uniqueness_of a call to the server must be made. You can do this by simply drop in the ClidenSideValidations Rack middleware.
47
+ If you want to validate_uniqueness_of a call to the server must be made. You can do this with the ClidenSideValidations::Uniqueness middleware.
48
48
 
49
49
  The following route will be reserved for client side validations:
50
50
 
@@ -91,6 +91,20 @@ That should be it!
91
91
 
92
92
  ## Advanced Options
93
93
 
94
+ ### Initialization
95
+ [jquery.validate can be customized by setting various options](http://docs.jquery.com/Plugins/Validation/validate#toptions)
96
+
97
+ Create config/initializers/client_side_validations.rb
98
+
99
+ An example set of default options can look like:
100
+
101
+ ClientSideValidations.default_options = {
102
+ :onkeyup => "false",
103
+ :errorClass => %{"validation_errors"}
104
+ }
105
+
106
+ You'll notice that for :errorClass a string is being set inside of a string. This is necessary so everything gets translated to Javascript properly. (hopefully I can come up with something nicer soon)
107
+
94
108
  ### Model
95
109
  If you want to define only specific fields for client side validations just override the validation_fields method on each model
96
110
 
@@ -106,6 +120,22 @@ If you want to define only specific fields for client side validations just over
106
120
  end
107
121
 
108
122
 
123
+ ### View
124
+ You can override the default options set in the initializer for each form:
125
+
126
+ <% form_for @book, :validations => { :options => { :errorClass => %{"bad-field"} } } do |b| %>
127
+ ...
128
+
129
+ Same rules apply of a string inside a string for proper conversion to Javascript.
130
+
131
+ If you are not using an instance variable for form_for or for some reason want to use the validations from another class that can be done in two ways:
132
+
133
+ <% form_for :book, :validations => Book %>
134
+ ...
135
+
136
+ <% form_for :book, :validations => { :class => Book } %>
137
+ ...
138
+
109
139
  Written by Brian Cardarella
110
140
 
111
141
  Copyright (c) 2010 Democratic National Committee. See LICENSE for details.
@@ -11,24 +11,26 @@ module DNCLabs
11
11
  def validation_to_hash(_attr, _options = {})
12
12
  validation_hash = {}
13
13
  base._validators[_attr.to_sym].each do |validation|
14
- message = get_validation_message(validation, _options[:locale])
15
- validation.options.delete(:message)
16
- options = get_validation_options(validation.options)
17
- method = get_validation_method(validation.kind)
18
- if conditional_method = remove_reserved_conditionals(options['if'])
19
- if base.instance_eval(conditional_method.to_s)
14
+ if can_validate?(validation)
15
+ message = get_validation_message(validation, _options[:locale])
16
+ validation.options.delete(:message)
17
+ options = get_validation_options(validation.options)
18
+ method = get_validation_method(validation.kind)
19
+ if conditional_method = remove_reserved_conditionals(options['if'])
20
+ if base.instance_eval(conditional_method.to_s)
21
+ options.delete('if')
22
+ validation_hash[method] = { 'message' => message }.merge(options)
23
+ end
24
+ elsif conditional_method = options['unless']
25
+ unless base.instance_eval(conditional_method.to_s)
26
+ options.delete('unless')
27
+ validation_hash[method] = { 'message' => message }.merge(options)
28
+ end
29
+ else
20
30
  options.delete('if')
21
- validation_hash[method] = { 'message' => message }.merge(options)
22
- end
23
- elsif conditional_method = options['unless']
24
- unless base.instance_eval(conditional_method.to_s)
25
31
  options.delete('unless')
26
32
  validation_hash[method] = { 'message' => message }.merge(options)
27
33
  end
28
- else
29
- options.delete('if')
30
- options.delete('unless')
31
- validation_hash[method] = { 'message' => message }.merge(options)
32
34
  end
33
35
  end
34
36
 
@@ -40,6 +42,17 @@ module DNCLabs
40
42
  end
41
43
 
42
44
  private
45
+
46
+ def can_validate?(validation)
47
+ if on = validation.options.delete(:on)
48
+ on = on.to_sym
49
+ (on == :save) ||
50
+ (on == :create && base.new_record?) ||
51
+ (on == :update && !base.new_record?)
52
+ else
53
+ true
54
+ end
55
+ end
43
56
 
44
57
  def get_validation_message(validation, locale)
45
58
  default = case validation.kind
@@ -13,22 +13,24 @@ module DNCLabs
13
13
  def validation_to_hash(_attr, _options = {})
14
14
  validation_hash = {}
15
15
  base.class.reflect_on_validations_for(_attr).each do |validation|
16
- message = get_validation_message(validation, _options[:locale])
17
- validation.options.delete(:message)
18
- options = get_validation_options(validation.options)
19
- method = get_validation_method(validation.macro)
20
- if conditional_method = options['if']
21
- if base.instance_eval(conditional_method.to_s)
22
- options.delete('if')
16
+ if can_validate?(validation)
17
+ message = get_validation_message(validation, _options[:locale])
18
+ validation.options.delete(:message)
19
+ options = get_validation_options(validation.options)
20
+ method = get_validation_method(validation.macro)
21
+ if conditional_method = options['if']
22
+ if base.instance_eval(conditional_method.to_s)
23
+ options.delete('if')
24
+ validation_hash[method] = { 'message' => message }.merge(options)
25
+ end
26
+ elsif conditional_method = options['unless']
27
+ unless base.instance_eval(conditional_method.to_s)
28
+ options.delete('unless')
29
+ validation_hash[method] = { 'message' => message }.merge(options)
30
+ end
31
+ else
23
32
  validation_hash[method] = { 'message' => message }.merge(options)
24
33
  end
25
- elsif conditional_method = options['unless']
26
- unless base.instance_eval(conditional_method.to_s)
27
- options.delete('unless')
28
- validation_hash[method] = { 'message' => message }.merge(options)
29
- end
30
- else
31
- validation_hash[method] = { 'message' => message }.merge(options)
32
34
  end
33
35
  end
34
36
 
@@ -40,6 +42,17 @@ module DNCLabs
40
42
  end
41
43
 
42
44
  private
45
+
46
+ def can_validate?(validation)
47
+ if on = validation.options.delete(:on)
48
+ on = on.to_sym
49
+ (on == :save) ||
50
+ (on == :create && base.new_record?) ||
51
+ (on == :update && !base.new_record?)
52
+ else
53
+ true
54
+ end
55
+ end
43
56
 
44
57
  def get_validation_message(validation, locale)
45
58
  default = case validation.macro.to_s
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 2
7
- - 3
7
+ - 4
8
8
  - 0
9
- version: 2.3.0
9
+ version: 2.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Brian Cardarella
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-07-09 00:00:00 -04:00
17
+ date: 2010-07-10 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency