|
1 | 1 | module Html5Validators |
2 | 2 | module ActionViewExtension |
3 | | - def inject_required_attribute |
4 | | - if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false) |
5 | | - @options["required"] ||= @options[:required] || object.class.attribute_required?(@method_name) |
| 3 | + module FormHelper |
| 4 | + def form_for(record, options = {}, &block) |
| 5 | + if record.respond_to?(:auto_html5_validation=) |
| 6 | + if !Html5Validators.enabled || (options[:auto_html5_validation] == false) |
| 7 | + record.auto_html5_validation = false |
| 8 | + end |
| 9 | + end |
| 10 | + super |
6 | 11 | end |
7 | 12 | end |
8 | 13 |
|
9 | | - def inject_maxlength_attribute |
10 | | - if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false) |
11 | | - @options["maxlength"] ||= @options[:maxlength] || object.class.attribute_maxlength(@method_name) |
| 14 | + module PresenceValidator |
| 15 | + def render |
| 16 | + if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false) |
| 17 | + @options["required"] ||= @options[:required] || object.class.attribute_required?(@method_name) |
| 18 | + end |
| 19 | + super |
12 | 20 | end |
13 | 21 | end |
14 | 22 |
|
15 | | - def inject_numericality_attributes |
16 | | - if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false) |
17 | | - @options["max"] ||= @options["max"] || @options[:max] || object.class.attribute_max(@method_name) |
18 | | - @options["min"] ||= @options["min"] || @options[:min] || object.class.attribute_min(@method_name) |
| 23 | + module LengthValidator |
| 24 | + def render |
| 25 | + if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false) |
| 26 | + @options["maxlength"] ||= @options[:maxlength] || object.class.attribute_maxlength(@method_name) |
| 27 | + end |
| 28 | + super |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + module NumericalityValidator |
| 33 | + def render |
| 34 | + if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false) |
| 35 | + @options["max"] ||= @options["max"] || @options[:max] || object.class.attribute_max(@method_name) |
| 36 | + @options["min"] ||= @options["min"] || @options[:min] || object.class.attribute_min(@method_name) |
| 37 | + end |
| 38 | + super |
19 | 39 | end |
20 | 40 | end |
21 | 41 | end |
22 | 42 | end |
23 | 43 |
|
24 | 44 |
|
25 | 45 | module ActionView |
26 | | - module Helpers |
27 | | - module FormHelper |
28 | | - def form_for_with_auto_html5_validation_option(record, options = {}, &proc) |
29 | | - if record.respond_to?(:auto_html5_validation=) |
30 | | - if !Html5Validators.enabled || (options[:auto_html5_validation] == false) |
31 | | - record.auto_html5_validation = false |
32 | | - end |
33 | | - end |
34 | | - form_for_without_auto_html5_validation_option record, options, &proc |
35 | | - end |
36 | | - alias_method_chain :form_for, :auto_html5_validation_option |
37 | | - end |
| 46 | + Base.send :prepend, Html5Validators::ActionViewExtension::FormHelper |
38 | 47 |
|
| 48 | + module Helpers |
39 | 49 | module Tags |
40 | | - class Base #:nodoc: |
41 | | - include Html5Validators::ActionViewExtension |
42 | | - end |
43 | | - |
44 | 50 | class TextField |
45 | | - def render_with_html5_attributes |
46 | | - inject_required_attribute |
47 | | - inject_maxlength_attribute |
48 | | - inject_numericality_attributes |
49 | | - |
50 | | - render_without_html5_attributes |
51 | | - end |
52 | | - alias_method_chain :render, :html5_attributes |
| 51 | + prepend Html5Validators::ActionViewExtension::NumericalityValidator |
| 52 | + prepend Html5Validators::ActionViewExtension::LengthValidator |
| 53 | + prepend Html5Validators::ActionViewExtension::PresenceValidator |
53 | 54 | end |
54 | 55 |
|
55 | 56 | class TextArea |
56 | | - def render_with_html5_attributes |
57 | | - inject_required_attribute |
58 | | - inject_maxlength_attribute |
59 | | - |
60 | | - render_without_html5_attributes |
61 | | - end |
62 | | - alias_method_chain :render, :html5_attributes |
| 57 | + prepend Html5Validators::ActionViewExtension::LengthValidator |
| 58 | + prepend Html5Validators::ActionViewExtension::PresenceValidator |
63 | 59 | end |
64 | 60 |
|
65 | 61 | #TODO probably I have to add some more classes here |
66 | 62 | [RadioButton, CheckBox, Select, DateSelect, TimeZoneSelect].each do |kls| |
67 | | - kls.class_eval do |
68 | | - def render_with_html5_attributes |
69 | | - inject_required_attribute |
70 | | - render_without_html5_attributes |
71 | | - end |
72 | | - alias_method_chain :render, :html5_attributes |
73 | | - end |
| 63 | + kls.send :prepend, Html5Validators::ActionViewExtension::PresenceValidator |
74 | 64 | end |
75 | 65 | end |
76 | 66 | end |
|
0 commit comments