simple_form and hstore basic functionality

筅森魡賤 提交于 2019-12-06 13:32:00

You can find an example of how to add some custom validations for Hstore here:

https://gist.github.com/rf-/2322543

module HstoreValidation
  def validates_hstore(field, &block)
    validation_class = Class.new do
      include ActiveModel::Validations

      def self.name
        '(validations)'
      end

      def initialize(data)
        @data = data
      end

      def read_attribute_for_validation(attr_name)
        @data[attr_name]
      end
    end
    validation_class.class_eval &block

    validate do
      validator = validation_class.new(self[field])

      if validator.invalid?
        validator.errors.each do |attr, text|
          self.errors.add(attr, text)
        end
      end
    end
  end
end

But as for how to get the validations to work with Simple form, I'm not sure.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!