使用 client_side_validation gem 中除 label 之外的任何标签时,Rails 字段错误过程无法正常工作
我有一个 Rails 3.0.10 项目,它使用 client_side_validations gem 在表单提交之前验证每个字段的表单输入。我使用 field_error_proc 块来覆盖 field_with_errors 输出,为每个字段提供内联消息。该代码确实有效,但是 label 标记的使用限制了可以完成的样式数量。我想以某种方式设置消息的样式,但它不允许我使用除 label 之外的任何其他标签...而不是 span 或 div。
如果我将标签更改为标签以外的其他内容,则名为“instance”的变量具有空白属性。我尝试将值分配给其他变量,但这也不起作用......如果我使用标签标签,它似乎只有正确的值。我完全不知道这怎么会这么脆弱。下面是我正在使用的代码:
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
unless html_tag =~ /^<label/
%{
<div class="field_with_errors">
<label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label>
#{html_tag}
</div>
}.html_safe
else
%{
<div class="field_with_errors">
#{html_tag}
</div>
}.html_safe
end
end
I have a Rails 3.0.10 project that is using the client_side_validations gem to validate form input on each field before form submission. I am using the field_error_proc block to override the field_with_errors output to give inline messages for each field. This code does work, but the use of the label tag limits the amount of styling that can be done. I would like to style the message in some way but it will NOT allow me to use any other tag other than label... not span or div.
If I change the tag to something other than label the variable named "instance" has blank attributes. I have tried to assign the values to other variables but this also does not work...it only seems to have the proper values if I use a label tag. I have absolutely no idea how this could be so brittle. Below is the code I am using:
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
unless html_tag =~ /^<label/
%{
<div class="field_with_errors">
<label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label>
#{html_tag}
</div>
}.html_safe
else
%{
<div class="field_with_errors">
#{html_tag}
</div>
}.html_safe
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论