I'm trying to validate that a has_many-through relationship has at least one value selected upon form submission. For simplicity, let's just call the relationship "relationship", and thus the ids "relationship_ids".
On my model, I included the following:
attr_accessible :relationship_ids
validates :relationship_ids, :length => {:minimum => 1}
Unfortunately, this does not work, as Rails forms includes an empty string in the array (i.e.: [""]
) in case the user selects nothing, such that Rails knows to remove all associations that were set previously. There is no error, it's just the the length of relationship_ids
is 1, and so the validation succeeds.
My next thought was that I could override the implementation of the relationship_ids=
method, so I tried this:
def relationship_ids=(ids)
super ids.reject(&:blank?)
end
Unfortunately, this results in a NoMethodError, specifically:
super: no superclass method `relationship_ids='
I'm thinking there's got to be a better/more correct way o开发者_C百科f doing this, and am looking for some input here. Thanks!
Edit: I already had a custom validator I was using previously. I've updated it to account for empty strings in the ids
array. Here it is, in case this helps anyone else out.
class RelationshipValidator < ActiveModel::EachValidator
CHECKS = { :is => :==, :minimum => :>=, :maximum => :<= }.freeze
MESSAGES = { :is => :equal_to, :minimum => :greater_than_or_equal_to, :maximum => :less_than_or_equal_to }.freeze
RESERVED_OPTIONS = [:minimum, :maximum, :within, :is, :greater_than_or_equal_to, :less_than_or_equal_to]
def initialize(options)
if range = (options.delete(:in) || options.delete(:within))
raise ArgumentError, ":in and :within must be a Range" unless range.is_a?(Range)
options[:minimum], options[:maximum] = range.begin, range.end
options[:maximum] -= 1 if range.exclude_end?
end
super(options)
end
def check_validity!
keys = CHECKS.keys & options.keys
if keys.empty?
raise ArgumentError, 'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.'
end
keys.each do |key|
value = options[key]
unless value.is_a?(Integer) && value >= 0
raise ArgumentError, ":#{key} must be a nonnegative Integer"
end
end
end
def validate_each(record, attribute, value)
value = record.send(attribute.to_sym).reject(&:blank?).size
CHECKS.each do |key, validity_check|
next unless check_value = options[key]
next if value && value.send(validity_check, check_value)
errors_options = options.except(*RESERVED_OPTIONS)
errors_options[:count] = check_value
default_message = options[MESSAGES[key]]
errors_options[:message] ||= default_message if default_message
record.errors.add(attribute, MESSAGES[key], errors_options)
end
end
end
And to use it, here are a few examples:
validate :relationship_ids, :relationship => {:minimum => 1}
validate :relationship_ids, :relationship => {:maximum => 5}
validate :relationship_ids, :relationship => {:is => 2}
validate :relationship_ids, :relationship => {:within => 1..3}
As noted (here), this Custom Rails Validations guide may help.
精彩评论