simple_form_with_client_validation 0.0.0 → 0.0.2

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/README.md CHANGED
@@ -776,7 +776,7 @@ https://github.com/Wbunker/Simple-Form-With-Client-Validation/issues
776
776
  ## Maintainers
777
777
 
778
778
  * Will Bunker (https://github.com/Wbunker)
779
- * Scott Levy
779
+ * Scott Levy (https://github.com/slevy)
780
780
 
781
781
  ## License
782
782
 
@@ -17,6 +17,11 @@ module SimpleFormWithClientValidation
17
17
  directory 'config'
18
18
  end
19
19
 
20
+ def copy_javascript
21
+ copy_file "simple_form_with_client_extensions.js", "app/assets/javascripts/simple_form_with_client_extensions.js"
22
+ puts "Need to add //= require simple_form_with_client_extensions to your application.js file in apps/assets/applications file"
23
+ end
24
+
20
25
  def copy_scaffold_template
21
26
  engine = options[:template_engine]
22
27
  copy_file "_form.html.#{engine}", "lib/templates/#{engine}/scaffold/_form.html.#{engine}"
@@ -0,0 +1,112 @@
1
+ /*
2
+ JQuery validation plugin code
3
+ */
4
+ jQuery.validator.addMethod(
5
+ "regex",
6
+ function(value, element, regexp) {
7
+ return this.optional(element) || regexp.test(value);
8
+ },
9
+ "Please check your input."
10
+ );
11
+
12
+ //must build rules based on html tags
13
+ jQuery(document).ready(function() {
14
+
15
+ var $ = jQuery;
16
+ $("form.simple_form").each(function(index, el) {
17
+ var rules = {};
18
+ var messages = {};
19
+
20
+ var findErrorMessage= function(rule_name, rule_type, el, message_attribute, default_error_message ) {
21
+
22
+ //make sure we have a messages section
23
+ if (typeof messages[rule_name] === 'undefined') {
24
+ messages[rule_name] = {};
25
+ }
26
+
27
+ //grab the message to create message
28
+ //user sees if this pattern doesn't match
29
+ var message = el.attr(message_attribute);
30
+ if (message !== null && typeof message !== 'undefined') {
31
+ messages[rule_name][rule_type] = message;
32
+ } else if (typeof default_error_message !== 'undefined'){
33
+ messages[rule_name][rule_type] = default_error_message;
34
+ }
35
+ }
36
+
37
+ var findInputName = function(el) {
38
+ var inputName = el.attr('name');
39
+ if (typeof rules[inputName] === 'undefined') {
40
+ rules[inputName] = {};
41
+ }
42
+ return inputName;
43
+
44
+ }
45
+
46
+
47
+ // handle all required files
48
+ $(el).find("input.required").each(function(indexRequired, requiredEl) {
49
+
50
+ requiredEl = $(requiredEl);
51
+
52
+ //create a rule for this element
53
+ var inputName = findInputName(requiredEl);
54
+ requiredEl.data("has_validator", true);
55
+ rules[inputName]["required"] = true;
56
+
57
+ //grab the data-pattern-message to create message
58
+ //user sees if this pattern doesn't match
59
+ findErrorMessage(inputName, "required", requiredEl, 'data-required-message', "This is required" )
60
+
61
+ });
62
+
63
+
64
+ //handle all regular expression by looking for data-pattern
65
+ $(el).find("input[data-pattern]").each(function(indexRegex, inputEle) {
66
+
67
+ var regExpEl = $(inputEle);
68
+ var regExprString = regExpEl.attr("data-pattern");
69
+ var regExpr = null;
70
+
71
+ //tries to create RegExp object and gives up if this fails
72
+ try {
73
+ regExpr = new RegExp(regExprString);
74
+ } catch(e) {
75
+ //can't build rule with this regular expression
76
+ console.log("couldn't build validator for data-pattern=" + regExprString)
77
+ return;
78
+ }
79
+
80
+ //create a rule for this element
81
+ var inputName = findInputName(regExpEl);
82
+ rules[inputName]["regex"] = regExpr;
83
+ regExpEl.data("has_validator", true);
84
+
85
+ //grab the data-pattern-message to create message
86
+ //user sees if this pattern doesn't match
87
+ findErrorMessage(inputName, "regex", regExpEl, 'data-pattern-message' )
88
+ })
89
+
90
+ var validateOptions = {
91
+ onfocusout: function(element) {
92
+ element = $(element);
93
+
94
+ //don't do anything if not marked for validation
95
+ if (element.data("has_validator") !== true) {
96
+ return;
97
+ }
98
+
99
+ var isValid = $(element).valid();
100
+ if (!isValid){
101
+ $(element).focus();
102
+ }
103
+ },
104
+ rules: rules,
105
+ messages: messages,
106
+ errorLabelContainer: "form.simple_form .errorArea"
107
+ };
108
+ $(el).validate(validateOptions)
109
+ })
110
+
111
+ })
112
+
@@ -1,20 +1,39 @@
1
+ require 'simple_form_with_client_validation/core_ext/regexp'
2
+
1
3
  module SimpleFormWithClientValidation
2
4
  module Components
3
5
  # Needs to be enabled in order to do automatic lookups.
4
6
  module Pattern
5
7
  def pattern
6
8
  input_html_options[:pattern] ||= pattern_source
9
+ input_html_options[:'data-pattern'] = input_html_options[:pattern]
10
+ input_html_options[:'data-pattern-message'] = options[:'data-pattern-message'] || pattern_error_message
7
11
  nil
8
12
  end
9
13
 
10
14
  private
11
15
 
16
+ def pattern_error_message
17
+
18
+ pattern_error = options[:'data-pattern-message']
19
+ if pattern_error.is_a?(String)
20
+ pattern_error
21
+ elsif pattern_validator = find_pattern_validator
22
+ if pattern_validator.options.has_key?(:message)
23
+ pattern_validator.options[:message]
24
+ else
25
+ ""
26
+ end
27
+ end
28
+ end
29
+
12
30
  def pattern_source
13
31
  pattern = options[:pattern]
14
32
  if pattern.is_a?(String)
15
33
  pattern
16
34
  elsif pattern_validator = find_pattern_validator
17
- evaluate_format_validator_option(pattern_validator.options[:with]).source
35
+ pattern = evaluate_format_validator_option(pattern_validator.options[:with]).to_javascript
36
+ pattern.slice(1,pattern.length-2)
18
37
  end
19
38
  end
20
39
 
@@ -0,0 +1,7 @@
1
+ # this came from
2
+ # http://reefpoints.dockyard.com/ruby/2011/11/18/convert-ruby-regexp-to-javascript-regex.html
3
+ class Regexp
4
+ def to_javascript
5
+ Regexp.new(inspect.sub('\\A','^').sub('\\Z','$').sub('\\z','$').sub(/^\//,'').sub(/\/[a-z]*$/,'').gsub(/\(\?#.+\)/, '').gsub(/\(\?-\w+:/,'('), self.options).inspect
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module SimpleFormWithClientValidation
2
- VERSION = "0.0.0".freeze
2
+ VERSION = "0.0.2".freeze
3
3
  end
@@ -242,17 +242,6 @@ class BuilderTest < ActionView::TestCase
242
242
  end
243
243
  end
244
244
 
245
- test 'collection_radio helper is deprecated in favor of collection_radio_buttons' do
246
- assert_deprecated "[SIMPLE_FORM] The `collection_radio` helper is deprecated, " \
247
- "please use `collection_radio_buttons` instead" do
248
- with_concat_form_for(@user) do |f|
249
- f.collection_radio :active, [true, false], :to_s, :to_s
250
- end
251
- end
252
-
253
- assert_select 'input[type=radio][value=true]'
254
- assert_select 'input[type=radio][value=false]'
255
- end
256
245
 
257
246
  # COLLECTION CHECK BOX
258
247
  test 'collection check box accepts a collection and generate a serie of checkboxes for value method' do
@@ -82,6 +82,21 @@ class StringInputTest < ActionView::TestCase
82
82
  assert_select 'input[pattern="\w+"]'
83
83
  end
84
84
 
85
+ test 'input should infer data-pattern from attributes' do
86
+ with_input_for @other_validating_user, :country, :string, :pattern => true
87
+ assert_select 'input[data-pattern="\w+"]'
88
+ end
89
+
90
+ test 'input should infer data-pattern-message from attributes' do
91
+ with_input_for @other_validating_user, :country, :string, :pattern => true
92
+ assert_select 'input[data-pattern-message="Country is wrong format."]'
93
+ end
94
+
95
+ test 'input should infer emptyp data-pattern-message from attributes' do
96
+ with_input_for @other_validating_user, :name, :string, :pattern => true
97
+ assert_select 'input[data-pattern-message=""]'
98
+ end
99
+
85
100
  test 'input should infer pattern from attributes using proc' do
86
101
  with_input_for @other_validating_user, :name, :string, :pattern => true
87
102
  assert_select 'input[pattern="\w+"]'
@@ -188,7 +188,7 @@ class OtherValidatingUser < User
188
188
  :less_than_or_equal_to => Proc.new { |user| user.age + 100},
189
189
  :only_integer => true
190
190
 
191
- validates_format_of :country, :with => /\w+/
191
+ validates_format_of :country, :with => /\w+/, :message=>"Country is wrong format."
192
192
 
193
193
  # TODO: Remove this check when we drop Rails 3.0 support
194
194
  if ActiveModel::VERSION::MAJOR == 3 && ActiveModel::VERSION::MINOR >= 1
data/test/test_helper.rb CHANGED
@@ -60,7 +60,7 @@ class ActionView::TestCase
60
60
 
61
61
  @validating_user = ValidatingUser.new({
62
62
  :id => 1,
63
- :name => 'New in SimpleFormWithClientValidation!',
63
+ :name => 'New in SimpleForm!',
64
64
  :description => 'Hello!',
65
65
  :home_picture => 'Home picture',
66
66
  :created_at => Time.now,
metadata CHANGED
@@ -1,21 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_form_with_client_validation
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 0
10
- version: 0.0.0
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Will Bunker
14
+ - Scott Levy
14
15
  autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
18
 
18
- date: 2012-03-12 00:00:00 Z
19
+ date: 2012-03-17 00:00:00 Z
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: activemodel
@@ -66,6 +67,7 @@ files:
66
67
  - lib/generators/simple_form_with_client_validation/templates/config/initializers/simple_form.rb.tt
67
68
  - lib/generators/simple_form_with_client_validation/templates/config/locales/simple_form.en.yml
68
69
  - lib/generators/simple_form_with_client_validation/templates/README
70
+ - lib/generators/simple_form_with_client_validation/templates/simple_form_with_client_extensions.js
69
71
  - lib/generators/simple_form_with_client_validation/USAGE
70
72
  - lib/simple_form_with_client_validation/action_view_extensions/builder.rb
71
73
  - lib/simple_form_with_client_validation/action_view_extensions/form_helper.rb
@@ -82,6 +84,7 @@ files:
82
84
  - lib/simple_form_with_client_validation/components/readonly.rb
83
85
  - lib/simple_form_with_client_validation/components.rb
84
86
  - lib/simple_form_with_client_validation/core_ext/hash.rb
87
+ - lib/simple_form_with_client_validation/core_ext/regexp.rb
85
88
  - lib/simple_form_with_client_validation/error_notification.rb
86
89
  - lib/simple_form_with_client_validation/form_builder.rb
87
90
  - lib/simple_form_with_client_validation/helpers/autofocus.rb
@@ -154,7 +157,7 @@ files:
154
157
  - test/support/mock_response.rb
155
158
  - test/support/models.rb
156
159
  - test/test_helper.rb
157
- homepage: https://github.com/plataformatec/simple_form
160
+ homepage: https://github.com/Wbunker/Simple-Form-With-Client-Validation
158
161
  licenses: []
159
162
 
160
163
  post_install_message: