Module: LiveValidator::ViewHelpers

Defined in:
lib/live_validator/view_helpers.rb

Constant Summary collapse

G_VALIDATION_METHODS =
{
  :presence => "Validate.Presence",
  :numericality => "Validate.Numericality",
  :format => "Validate.Format",
  :length => "Validate.Length",
  :size => "Validate.Length",
  :acceptance => "Validate.Acceptance",
  :confirmation => "Validate.Confirmation",
  :exclusion => "Validate.Exclusion"
}

Instance Method Summary collapse

Instance Method Details

#g_live_validator(form, *args) ⇒ Object

Guilded component. This reads from the server side validations and sets up client side validations that match utilizing the Live Validation library. The following validation macros and args are implemented:

validates_presence_of - :message validates_numericality_of - :less_than, :less_than_or_equal_to, :equal_to, :greater_than,

:greater_then_or_equal_to, :only_integer, :notANumberMessage, :notAnIntegerMessage,
:wrongNumberMessage, :tooLowMessage, :tooHighMessage

validates_length_of / validates_size_of - :maximum, :minimum, :is, :within, :in, :too_long,

:too_short, :wrong_length

validates_confirmation_of - Only works for 2 fields, no more or less. validates_acceptance_of - :message validates_inclusion_of - :message validates_exclusion_of - :message

If you need to do custom initialization you can implement g.beforeLiveValidatorInit() or g.afterLiveValidatorInit().

If you need custom handling of valid or invalid fields, you can implement g.liveValidatorInvalidField() or g. liveValidatorValidField().

parameters

form

The form object from the form_for helper.

options

id

(required)

except

List of fields to not include. A string, symbol or array of strings or symbols.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/live_validator/view_helpers.rb', line 43

def g_live_validator( form, *args )
  options = args.extract_options!
  klass = form.object.class
  class_name = klass.to_s.downcase
  options.merge! :id => "live-validator-#{class_name}"
  ar_obj_name = form.object.class.to_s.underscore
  
  validations = g_map_validations( klass.reflect_on_all_validations )
  
  # Remove any foreign keys as they will not be present on the form
  validations.reject! { |field, validation| field.include?( "_id" ) }
  
  # Remove any excepts, if necessary
  if options[:except]
    if options[:except].is_a?( Array )
      excepts = options[:except]
    elsif options[:except].is_a?( String ) || options[:except].is_a?( Symbol )
      excepts = Array.new << options[:except]          
    else
      throw "'Except' option must be a string symbol or arry of strings or symbols"
    end

    # Add the AR object name to the field name, as Rails does this on forms
    excepts.map! { |except| "#{ar_obj_name}_#{except.to_s}" }
    
    excepts.each do |except|
      validations.reject! { |field, validation| field == except }
    end
  end
  
  # Handle nested form namings, if necessary
  if form.object.class.to_s.underscore != form.object_name
    #field_precursor = form.object_name.gsub( /\[/, '_' ).gsub( /\]/, '_' )
    field_precursor = form.object_name[0...form.object_name.index( "[" )] + '_'
    nested_validations = Hash.new
    validations.each do |key, value|
      nested_validations[ "#{field_precursor}#{key}".to_sym ] = value
    end
    validations = nested_validations
  end
  
  options.merge! :validations => validations
  Guilded::Guilder.instance.add( :live_validator, options, [ 'livevalidation-1.3.compressed.js' ] )
  return ""
end