表单元素的自定义 HTML 错误包装器 [英] Custom HTML Error Wrappers for Form Elements

查看:35
本文介绍了表单元素的自定义 HTML 错误包装器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到一种方法来自定义默认错误 html

参加我自己的课程:

 

<label for="errorInput">输入错误</label><div class="输入"><input class="xlarge error" id="errorInput" name="errorInput" size="30" type="text"><span class="help-inline">帮助文本的小片段</span>

我相信我从 2007 年发现了这个使用 Rails 2 的 railscast.http://railscasts.com/episodes/39-customize-field-error.似乎 Rails 3 可能有更友好的方式来自定义这个 html?

此外,它并没有显示出一种直接向输入添加错误类的方法,就像我想要的那样.

解决方案

您发布的链接中解释的方法今天仍在使用 Rails 中的普通表单构建器.

所以,如果你想像你提到的那样包装你的输入,例如在你的 environment.rb 文件中创建一个覆盖 ActionView::Base.field_error_proc 的方法,像这样:

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|if instance.error_message.kind_of?(Array)%(<div class="form-field error">#{html_tag}<small class="error">&nbsp;#{instance.error_message.join(',')}</small></div).html_safe别的%(<div class="form-field error">#{html_tag}<small class="error">&nbsp;#{instance.error_message}</small></div).html_safe结尾结尾

在上面的代码中,我将我的输入(#{html_tag})包装在一个 <div class="form-field error>..</div> 这就是ZURB Foundation 使用的默认值.我也在使用 <small class="error">...</small 标记(这也是基础默认值)以显示输入下方的消息.

但是,我建议使用像 simple_form 这样的表单构建器 gem.它稍微清理了您的视图代码,并允许您进行所需的自定义级别.

此处查看有关 railscast 的内容.

祝你好运!

I would like to find a way to customize the default error html

<div class="field_with_errors"></div>

To take my own classes:

     <div class="clearfix error">
        <label for="errorInput">Input with error</label>
        <div class="input">
          <input class="xlarge error" id="errorInput" name="errorInput" size="30" type="text">
          <span class="help-inline">Small snippet of help text</span>
        </div>
      </div>

I have found this railscast from 2007 which uses Rails 2, I believe. http://railscasts.com/episodes/39-customize-field-error. It seems like Rails 3 might have a more friendly way to customize this html?

Also, it doesn't show a way to just add an error class directly to the input like I want.

解决方案

The method explained in the link you posted is still used today with the vanilla form builders in Rails.

So, if you wanted to wrap your input like you mention, create a method overriding the ActionView::Base.field_error_proc in your environment.rb file for example, like so:

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  if instance.error_message.kind_of?(Array)
    %(<div class="form-field error">#{html_tag}<small class="error">&nbsp;
      #{instance.error_message.join(',')}</small></div).html_safe
  else
    %(<div class="form-field error">#{html_tag}<small class="error">&nbsp;
      #{instance.error_message}</small></div).html_safe
  end
end

In the above code, I'm wrapping my input (the #{html_tag}) in a <div class="form-field error>..</div> that's the default used by ZURB Foundation. I'm also using a <small class="error">...</small tag (which is also the foundation default) to display the messages below the input.

However, I recommend using a form builder gem like simple_form. It cleans up your view code quit a bit and allows for the level of customization you require.

Check out the railscast on it here.

Good luck!

这篇关于表单元素的自定义 HTML 错误包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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