Rails:验证至少一种 HABTM 关系

发布于 2024-11-06 04:42:55 字数 2839 浏览 5 评论 0原文

我正在尝试验证 has_many-through 关系是否在提交表单时至少选择了一个值。为了简单起见,我们将关系称为“relationship”,因此将 id 称为“relationship_ids”。

在我的模型中,我添加了以下内容:

attr_accessible :relationship_ids
validates :relationship_ids, :length => {:minimum => 1}

不幸的是,这不起作用,因为 Rails 表单在数组中包含一个空字符串(即:[""]),以防用户选择任何内容,这样Rails 知道删除之前设置的所有关联。没有错误,只是relationship_ids的长度为1,所以验证成功。

我的下一个想法是我可以覆盖 relationship_ids= 方法的实现,因此我尝试了以下方法:

def relationship_ids=(ids)
  super ids.reject(&:blank?)
end

不幸的是,这会导致 NoMethodError,具体来说:

super:没有超类方法`relationship_ids='

我认为必须有一个更好/更正确的方法来做到这一点,并且我在这里寻找一些输入。谢谢!

编辑:我之前已经有一个自定义验证器。我已将其更新为考虑到 ids 数组中的空字符串。就在这里,以防这对其他人有帮助。

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

要使用它,这里有一些示例:

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}

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 of 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}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七七 2024-11-13 04:42:55

如前所述(此处),此自定义 Rails 验证 指南可能会有所帮助。

As noted (here), this Custom Rails Validations guide may help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文