#721 ✓resolved
Roman

[PATCH] allow nil values for within validator

Reported by Roman | December 19th, 2008 @ 09:46 AM

Similary to numeric_validator added option :allow_nil to within_validator:


diff --git a/dm-validations/lib/dm-validations/within_validator.rb b/dm-validations/li
index 583363e..018440b 100644
--- a/dm-validations/lib/dm-validations/within_validator.rb
+++ b/dm-validations/lib/dm-validations/within_validator.rb
@@ -14,8 +14,11 @@ module DataMapper
       end
 
       def call(target)
-        includes = @options[:set].include?(target.send(field_name))
-        return true if includes
+        value = target.send(field_name)
+        return true if @options[:allow_nil] && value.nil?
+
+        includes = @options[:set].include?(value)
+        return true if includes || target.send(field_name).nil?
 
         field_name = Extlib::Inflection.humanize(@field_name)
         if @options[:set].is_a?(Range)

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket

Pages