代码之家  ›  专栏  ›  技术社区  ›  Lee McAlilly

如何更改默认rails错误div“field_with_errors”

  •  25
  • Lee McAlilly  · 技术社区  · 14 年前

    我正在使用 sorcery 用于身份验证以及 twitter bootstrap 。

    我想通过更改默认轨道,以twitter引导程序的风格在注册表单上设置错误消息的样式 <div class="field_with_errors"> 添加到DOM中。

    做这样的事情的铁路惯例是什么?

    我想您可以添加一些javascript来操纵DOM来重命名 <div class=“field_with_errors”> ,但这似乎是个黑客。似乎应该有一种方法可以在铁轨上覆盖这一点,但我不知道在哪里可以做到。

    这就是引导程序要求您标记错误以使用其内置形式的错误样式的方式:

    <div class="control-group error">
      <label class="control-label" for="inputError">Input with error</label>
      <div class="controls">
        <input type="text" id="inputError">
        <span class="help-inline">Please correct the error</span>
      </div>
    </div>
    
    6 回复  |  直到 14 年前
        1
  •  32
  •   Un3qual Vikdor    12 年前

    从上面的链接中,如果您将以下内容放入 class Application < Rails::Application 属于 config/application.rb

    config.action_view.field_error_proc = Proc.new { |html_tag, instance| 
      "<div class=\"field_with_errors control-group error\">#{html_tag}</div>".html_safe
    }
    

    每当验证失败时,您的输入标签周围都会有一个红色标记

        2
  •  16
  •   Dex    6 年前

    使用Rails 6和Bootstrap 4,您需要添加 is-invalid 班没有太过花哨,我只是做了:

    ActionView::Base.field_error_proc = proc do |html_tag, instance|
      html_tag.gsub("form-control", "form-control is-invalid").html_safe
    end
    
        3
  •  1
  •   Community Mohan Dere    9 年前

    对于Bootstrap 3.2,你可以使用这样的东西(添加 nokogiri 宝石到您的宝石文件):

    ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
      html = %(<div class="field_with_errors">#{html_tag}</div>).html_safe
      # add nokogiri gem to Gemfile
    
      form_fields = [
        'textarea',
        'input',
        'select'
      ]
    
      elements = Nokogiri::HTML::DocumentFragment.parse(html_tag).css "label, " + form_fields.join(', ')
    
      elements.each do |e|
        if e.node_name.eql? 'label'
          html = %(<div class="control-group has-error">#{e}</div>).html_safe
        elsif form_fields.include? e.node_name
          if instance.error_message.kind_of?(Array)
            html = %(<div class="control-group has-error">#{html_tag}<span class="help-block">&nbsp;#{instance.error_message.uniq.join(', ')}</span></div>).html_safe
          else
            html = %(<div class="control-group has-error">#{html_tag}<span class="help-block">&nbsp;#{instance.error_message}</span></div>).html_safe
          end
        end
      end
      html
    end
    

    将此代码放入 config/initializers/field_error_proc.rb 文件(如果不存在,请创建一个)

    这是对以下代码的轻微修改: Overriding ActionView::Base.field_error_proc in Rails

        4
  •  0
  •   ronan_mac    12 年前

    请注意,如果您使用的是SimpleForm,那么在application.rb中使用Proc的公认答案将不起作用。相反,您应该编辑simple_form初始值设定项。例如,使用Bootstrap 3.2:

    config.wrappers :default, class: :input,
      hint_class: :field_with_hint, error_class: :'has-error' do |b|
      [...]
      b.use :hint,  wrap_with: { tag: :span, class: :'text-warning' }
      b.use :error, wrap_with: { tag: :span, class: :'text-danger' }
    end
    
        5
  •  0
  •   Fab V.    9 年前

    Rails 5上的Twitter Bootstrap 3.3.6,这是在初始化器中进行的 customize_error.rb 并为我工作:

    ActionView::Base.field_error_proc = proc { |html_tag, _instance| "<div class=\"has-error\">#{html_tag}</div>".html_safe }
    
        6
  •  0
  •   Tintin81    7 年前

    我只想关闭错误 复选框 ,所以我这样做了:

    ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
      doc = Nokogiri::HTML::Document.parse(html_tag)
      if doc.xpath("//*[@type='checkbox']").any?
        html_tag
      else
        "<div class=\"field_with_errors\">#{html_tag}</div>".html_safe
      end
    end