Module: Campo::Plugins::JQueryValidation::InstanceMethods::Convenience

Defined in:
lib/campo/plugins/jqueryvalidation.rb

Overview

the simplest validation possible

Instance Method Summary collapse

Instance Method Details

#validate(*args) ⇒ Object



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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/campo/plugins/jqueryvalidation.rb', line 55

def validate( *args )

  # TODO move this to helper
  label, field = if self.kind_of? Campo::Label
    [self,self.fields.first] # for the key            
  elsif self.parent.kind_of? Campo::Label
    [self.parent, self]  # for the key
  end
  
  key = field.attributes[:id] || field.attributes[:name]
  
  # required
  if args.empty? || args.include?( :required )
    [label, field].each do |elem|
      elem.attributes.merge!({ :class => "required" } )
    end
    Rules[key] = :required
  end
  
  unless args.empty?
    hashes = {}
    singles = []
    args.each do |x|
      if x.kind_of? Hash
        hashes.merge! x 
      else
        singles << x
      end
    end
    
    # TODO required letters/symbols/numbers
    
    if hashes.include? :minlength
      Rules[key] = {minlength: hashes[:minlength] }
    end
                      
    if singles.include?( :maxlength )          
      if field.attributes.include? :size
        Rules[key] = {maxlength: field.attributes[:size]}
      end
    end
    
    if hashes.include? :maxlength
      Rules[key] = {maxlength: hashes[:maxlength] }
    end
    # http://docs.jquery.com/Plugins/Validation/Methods/digits
    Rules[key] = :digits if singles.include? :digits
  end
  
  self
end