从活动记录错误查看页面更改验证错误清洁闪光灯消息 [英] change validation error from active record error view page to clean flash message

查看:241
本文介绍了从活动记录错误查看页面更改验证错误清洁闪光灯消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现下面的code在<一个href="http://stackoverflow.com/questions/5669496/how-do-i-validate-members-of-an-array-field/18732936#18732936">How我验证一个数组字段的成员?。

I found the following code at How do I validate members of an array field? .

# Validates the values of an Enumerable with other validators.
# Generates error messages that include the index and value of
# invalid elements.
#
# Example:
#
#   validates :values, enum: { presence: true, inclusion: { in: %w{ big small } } }
#
class EnumValidator < ActiveModel::EachValidator

  def initialize(options)
    super
    @validators = options.map do |(key, args)|
      create_validator(key, args)
    end
  end

  def validate_each(record, attribute, values)
    helper = Helper.new(@validators, record, attribute)
    Array.wrap(values).each do |value|
      helper.validate(value)
    end
  end

  private

  class Helper

    def initialize(validators, record, attribute)
      @validators = validators
      @record = record
      @attribute = attribute
      @count = -1
    end

    def validate(value)
      @count += 1
      @validators.each do |validator|
        next if value.nil? && validator.options[:allow_nil]
        next if value.blank? && validator.options[:allow_blank]
        validate_with(validator, value)
      end
    end

    def validate_with(validator, value)
      before_errors = error_count
      run_validator(validator, value)
      if error_count > before_errors
        prefix = "element #{@count} (#{value}) "
        (before_errors...error_count).each do |pos|
          error_messages[pos] = prefix + error_messages[pos]
        end
      end
    end

    def run_validator(validator, value)
      validator.validate_each(@record, @attribute, value)
    rescue NotImplementedError
      validator.validate(@record)
    end

    def error_messages
      @record.errors.messages[@attribute]
    end

    def error_count
      error_messages ? error_messages.length : 0
    end
  end

  def create_validator(key, args)
    opts = {attributes: attributes}
    opts.merge!(args) if args.kind_of?(Hash)
    validator_class(key).new(opts).tap do |validator|
      validator.check_validity!
    end
  end

  def validator_class(key)
    validator_class_name = "#{key.to_s.camelize}Validator"
    validator_class_name.constantize
  rescue NameError
    "ActiveModel::Validations::#{validator_class_name}".constantize
  end
end

这是工作我发现在验证轨道形成输入端,期待一个数组的唯一途径。现在的问题是,对于无效条目错误信息是不是在一个干净的闪光消息,但典型的Rails错误页面:

It is the only way that works i have found of validating a rails form input that is expecting an array. The problem is that the error message for invalid entries is not in a clean flash message but the typical rails error page:

例如,我的形式有多种选择输入字段,其中用户可以输入多个变量,从列表中。如果用户输入的标签是不是从列表中,我希望发生的验证和用户被告知,他们必须从列表中选择一个项目。如何更改错误信息到一个干净的闪信?

For example, my form has a multiple select input field in which the user can enter several tags from a list. If the user enters tags that are not from the list, i want the validation to occur and for the user to be told that they must pick an item from the list. How do I change the error message into a clean flash message?

推荐答案

看来,验证是提高仅仅是添加错误信息到错误堆栈的错误信息。我建议你​​包裹 update_attributes的块在开始/救援/ end`块。

It appears that the validator is raising an error instead of simply adding error messages to the errors stack. I'd recommend wrapping the update_attributes block in abegin/rescue/end` block.

有关更改邮件,你可以采取两种途径:

For changing the message, you can take two routes:

  1. 您可以自定义 EnumValidator 使用键从区域文件,使每个条目可能会说的元素%{数}不在列表。您应该能够自定义 activerecord.errors.messages.inclusion 该消息的不在名单部分。对于元素0(导轨)部分中,您既可以硬$ C C语$,作为目前执行(不推荐)或使用的语言环境的文件再次通过添加键说, enum_element ,然后在您的EnumValidator,电话 I18n.translate(:'activerecord.errors.messages.enum_element,数:@count,值:值)

  1. You could customize the EnumValidator to use keys from your locale file so that each entry might say something like the "element %{count} is not in the list". You should be able to customize activerecord.errors.messages.inclusion for the "is not in the list" portion of the message. For the "element 0 (rails)" portion, you could either hard-code the phrase, as is currently implemented (not recommended) or make use of the locale file again by adding a key for say, enum_element, then in your EnumValidator, call I18n.translate(:'activerecord.errors.messages.enum_element', count: @count, value: value).

验证后,检查 errors.keys 为您的枚举列问题。如果您发现错误,删除那些键,并添加自己的消息对于这把钥匙。

After validation, check errors.keys for for issues on your enumerable column. If you find errors, remove those keys and add your own message for that key.

至于你的形式,这听起来像您使用的是动态表单生成器。如果是SimpleForm,你可以尝试指定的确切类型的输入应与为::选择选项

As for your form, it sounds like you're using a dynamic form builder. If it's SimpleForm, you could try specifying the exact type the input should be with the as: :select option.

这篇关于从活动记录错误查看页面更改验证错误清洁闪光灯消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆