mirror of
https://github.com/zhigang1992/rmq.git
synced 2026-01-12 17:52:17 +08:00
added validation options at the selction level
This commit is contained in:
@@ -11,9 +11,9 @@ module RubyMotionQuery
|
||||
end
|
||||
|
||||
# @return [RMQ]
|
||||
def validates(rule)
|
||||
def validates(rule, options={})
|
||||
selected.each do |view|
|
||||
view.rmq_data.validations << Validation.new(rule)
|
||||
view.rmq_data.validations << Validation.new(rule, options)
|
||||
end
|
||||
self
|
||||
end
|
||||
@@ -53,13 +53,15 @@ module RubyMotionQuery
|
||||
|
||||
class Validation
|
||||
|
||||
def initialize(rule)
|
||||
def initialize(rule, options={})
|
||||
@rule = @@validation_methods[rule]
|
||||
@options = options
|
||||
raise "RMQ validation error: :#{rule} is not one of the supported validation methods." unless @rule
|
||||
end
|
||||
|
||||
def valid?(data, options={})
|
||||
@rule.call(data, options)
|
||||
@options = options.merge(@options)
|
||||
@rule.call(data, @options)
|
||||
end
|
||||
|
||||
class << self
|
||||
|
||||
@@ -128,11 +128,16 @@ describe 'validation' do
|
||||
vc.rmq.append(UITextField).validates(:digits).data('taco loco').tag(:one)
|
||||
vc.rmq.append(UITextField).validates(:digits).data('123455').tag(:two)
|
||||
vc.rmq.append(UITextField).validates(:digits).data('1234').tag(:three)
|
||||
# selections with options
|
||||
vc.rmq.append(UITextField).validates(:length, min_length: 2, max_length: 10).data('1234').tag(:four)
|
||||
vc.rmq.append(UITextField).validates(:length, exact_length: 5).data('1234').tag(:five)
|
||||
|
||||
vc.rmq.all.valid?.should == false
|
||||
vc.rmq(:one).valid?.should == false
|
||||
vc.rmq(:one, :two).valid?.should == false
|
||||
vc.rmq(:three, :two).valid?.should == true
|
||||
vc.rmq(:four).valid?.should == true
|
||||
vc.rmq(:five).valid?.should == false
|
||||
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user