【问题标题】:Rails 3: How not to include column's name in a validation message without plugins?Rails 3:如何不在没有插件的验证消息中包含列名?
【发布时间】:2011-05-31 03:18:46
【问题描述】:

我有以下验证:

validates_presence_of :price, :message => "my message"

当价格为空白时,我收到以下错误:

给我的消息定价

有没有办法不在消息中包含列名(price)?

我尝试过:

validates_presence_of :price, :message => "^ my message"

按照here 的建议,但它对我不起作用。我收到以下消息:

价格^我的消息

【问题讨论】:

    标签: ruby-on-rails validation ruby-on-rails-3


    【解决方案1】:

    ^ 技术在 Rails 3 的 dynamic_form gem 中定义。将它添加到您的 Gemfile,运行 bundle update,它应该可以工作。这也让您只需将 放入表单中即可避免格式化消息的大量额外工作。

    【讨论】:

      【解决方案2】:

      我认为最好和最灵活的方法是为表单字段编写自己的助手。显示用户输入错误因应用程序和布局而异。

      # lib/required_attributes.rb
      module ActiveModel
        module Validations
          module ClassMethods
      
            # ruby-1.9.2-head > Product.required_attributes
            #  => [:publisher_symbol_code, :price] 
            def required_attributes
              presence_validators = self.validators.find_all {|v| v.class == ActiveModel::Validations::PresenceValidator }
              presence_validators.map(&:attributes).flatten
            end
      
          end
        end
      end
      
      
      # application_helper.rb:
      
      def mark_as_required
        content_tag :span, '*', :class => :required
      end
      
      
      def form_field_for(object, *args, &block)
        options = args.extract_options!
        attributes = args.map(&:to_sym)
        field_contents = capture(&block)
        classes = "form-field clear #{options[:class]}"
      
        if options[:hint]
          field_contents << content_tag(:p, options[:hint], :class => :inline_hint)
        end
      
        # Are there any required attributes?
        any_attribute_required = (object.class.required_attributes & attributes).present?
      
        if options[:label]
          object_name = object.class.to_s.underscore  # 'product'
          label_contents = "#{options[:label]}: #{mark_as_required if any_attribute_required}".html_safe
          label_html = label(object_name, attributes.first, label_contents, :class => 'form-label fl-space2')
          field_contents = label_html + field_contents
        end
      
        errors = attributes.inject([]) do |mem, attrib|
          mem << object.errors[attrib]
          mem
        end.flatten.compact.uniq
      
        if errors.present?
          classes << ' error' 
          field_contents << content_tag(:p, errors.join(', '), :class => :inline_errors)
        end
      
        content_tag(:div, field_contents, :class => classes)
      end
      

      然后在视野中:

      = form_field_for @product, :publisher_symbol, :label => "Symbol", :hint => "pick a product symbol" do
          = product_form.text_field :publisher_symbol, :size => 70
      

      当有一组输入任何字段都可能出现错误时,您可以将它们分组如下:

      = form_field_for @product, :publication_year, :publication_month, :publication_day, :label => "Publication date" do
        = product_form.text_field :publication_year, :class => 'text fl', :size => 5
        = product_form.text_field :publication_month, :class => 'text fl', :size => 5
        = product_form.text_field :publication_day, :class => 'text fl', :size => 5
      

      发生错误时,您的视图如下所示:

      <div class="form-field clear error">
        <div class="field_with_errors">
          <label class="form-label fl-space2" for="product_publisher_symbol">Symbol: <span class="required">*</span></label>
        </div>
        <div class="field_with_errors">
          <input class="text fl" id="product_publisher_symbol" name="product[publisher_symbol]" size="70" type="text" value="">
        </div>                       
        <p class="inline_hint">pick a product symbol</p>
        <p class="inline_errors">can't be blank</p>
      </div>
      

      这段代码需要一些重构,但你明白了。如果您比我更懒惰并且不想在所有视图中输入“form_field_for”,那么创建一个自定义表单构建器:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-13
        • 2012-11-19
        • 1970-01-01
        • 2011-07-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多