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