【发布时间】:2016-05-20 17:05:46
【问题描述】:
我想将样式应用于成功验证的字段以及失败的字段。
我正在使用 fields_with_errors div,它应用于验证失败的表单元素。我想将此行为扩展到在成功验证的字段周围应用 fields_without_errors 样式的 div。
例如当 rails 验证字段时,渲染视图返回如下:
<div class="field_with_errors"><input class="input" size="30" type="text" value="" name="seller_profile[business_phone_number]" id="seller_profile_business_phone_number"></div>
原来的erb代码是:
<div class="row">
<label for="telephone">Company telephone</label>
<br />
<%=f.text_field :business_phone_number, :class=>"input", size:"30" %>
<div class="errors">
<%if @profile.errors[:business_phone_number].any? %>
Phone number <%= @profile.errors[:business_phone_number].join(", ")%>
<% end %>
</div>
<br />
</div>
这很好用。但是,对于已通过验证的字段,它们不会被包裹在 div 中,说明验证已通过。
我希望通过验证的字段如下所示:
<div class="field_without_errors"><input class="input" size="30" type="text" value="" name="seller_profile[business_description]" id="seller_profile_business_description"></div>
即它们被包装在一个带有“field_without_errors”类的 div 中。我有没有办法在 rails 中添加这种行为?
【问题讨论】:
-
定义简单。您必须找到一种方法来区分成功验证的字段和尚未验证的字段。而且您可能必须重写表单构建器类以不同方式处理成功验证的字段。
-
你也许可以用 css 做到这一点。请在您的问题中添加一个呈现出的 html 示例,该示例适用于混合了有效和无效输入的表单。如果rails添加的表单周围有一个div,带有错误类或其他内容,请确保包含它。
-
更新问题....
标签: html css ruby-on-rails ruby ruby-on-rails-4