lib/roda/component/form/validations.rb in roda-component-0.1.11 vs lib/roda/component/form/validations.rb in roda-component-0.1.15

- old
+ new

@@ -95,22 +95,34 @@ # the value of att with. # @param [Array<Symbol, Symbol>] error The error that should be returned # when the validation fails. def assert_format(att, format, error = [att, :format]) if assert_present(att, error) - assert(send(att).to_s.match(format), error) + assert(_attributes.send(att).to_s.match(format), error) end end # The most basic and highly useful assertion. Simply checks if the # value of the attribute is empty. # # @param [Symbol] att The attribute you wish to verify the presence of. # @param [Array<Symbol, Symbol>] error The error that should be returned # when the validation fails. - def assert_present(att, error = [att, :not_present]) - assert(!send(att).to_s.empty?, error) + def assert_present(att, error = [att, :not_present], klass = false) + if error.is_a? Class + error_new = klass.dup + klass = error + error = error_new || [att, :not_present] + + options = {} + options[:key] = _options[:key] if _options.key? :key + + f = klass.new(_attributes.send(att), options) + assert(f.valid?, [att, f.errors]) + else + assert(!_attributes.send(att).to_s.empty?, error) + end end # Checks if all the characters of an attribute is a digit. # # @param [Symbol] att The attribute you wish to verify the numeric format. @@ -137,16 +149,16 @@ assert_format(att, EMAIL, error) end end def assert_member(att, set, err = [att, :not_valid]) - assert(set.include?(send(att)), err) + assert(set.include?(_attributes.send(att)), err) end def assert_length(att, range, error = [att, :not_in_range]) if assert_present(att, error) - val = send(att).to_s + val = _attributes.send(att).to_s assert range.include?(val.length), error end end DECIMAL = /\A\-?(\d+)?(\.\d+)?\z/ @@ -171,10 +183,10 @@ # @param [Symbol] att The attribute you wish to verify for equality. # @param [Object] value The value you want to test against. # @param [Array<Symbol, Symbol>] error The error that should be returned # when the validation fails. def assert_equal(att, value, error = [att, :not_equal]) - assert value === send(att), error + assert value === _attributes.send(att), error end # The grand daddy of all assertions. If you want to build custom # assertions, or even quick and dirty ones, you can simply use this method. #