field_with_errors.

93 views
Skip to first unread message

Mauro

unread,
Oct 1, 2010, 11:07:30 AM10/1/10
to rubyonra...@googlegroups.com
If a model doesn't pass validation the field in the view is put under
a <div class="field_with_errors">.
But who create that div? field_text helper do it?

Butu

unread,
Oct 4, 2010, 10:05:59 PM10/4/10
to rubyonra...@googlegroups.com
Hello Mauro,

<div class="field_with_errors"> tag is created in ActionView::Base
class only in case of any errors.

From Rails code base:-

# Specify the proc used to decorate input tags that refer to
attributes with errors.
cattr_accessor :field_error_proc
@@field_error_proc = Proc.new{ |html_tag, instance| "<div
class=\"field_with_errors\">#{html_tag}</div>".html_safe }

Thanks!
Butu

> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>

Mauro

unread,
Oct 5, 2010, 3:07:28 AM10/5/10
to rubyonra...@googlegroups.com
On 5 October 2010 04:05, Butu <but...@gmail.com> wrote:
> Hello Mauro,
>
> <div class="field_with_errors"> tag is created in ActionView::Base
> class only in case of any errors.
>
> From Rails code base:-
>
>    # Specify the proc used to decorate input tags that refer to
> attributes with errors.
>    cattr_accessor :field_error_proc
>    @@field_error_proc = Proc.new{ |html_tag, instance| "<div
> class=\"field_with_errors\">#{html_tag}</div>".html_safe }
>
> Thanks!
> Butu

....and if I don't want those divs?

Colin Law

unread,
Oct 5, 2010, 3:27:21 AM10/5/10
to rubyonra...@googlegroups.com

For my education, why would you object to them being there?

Colin

pepe

unread,
Oct 5, 2010, 8:48:03 AM10/5/10
to Ruby on Rails: Talk
> > ....and if I don't want those divs?
>
> For my education, why would you object to them being there?

Something that has bothered me from time to time is that when there
are errors the <div> tag takes the whole line and makes the field to
end up in the next line, instead of appearing next to its label.
Another thing that annoys me is that if I create labels using the form
object (f.label ...) the label is also marked as in error, for that
reason only I don't use the form object to create labels.

I have never had the time to look for a solution but there might be an
easy one. It would be nice if one could choose between <div> and
<span>, for example.

Idonas

unread,
Oct 5, 2010, 7:44:18 AM10/5/10
to Ruby on Rails: Talk
Hi,

If you have your own way of handling errors in your views, you can
disable rails default encapsulation by doing

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
html_tag
end

inside an initializer.

and so no "field_with_errors" div will be wrapped around your fields.

On Oct 5, 10:27 am, Colin Law <clan...@googlemail.com> wrote:

Marnen Laibow-Koser

unread,
Oct 5, 2010, 9:47:28 AM10/5/10
to rubyonra...@googlegroups.com

.field_with_errors {
display: inline;
}

That should do the trick.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org
--
Posted via http://www.ruby-forum.com/.

pepe

unread,
Oct 5, 2010, 10:34:04 AM10/5/10
to Ruby on Rails: Talk
> .field_with_errors {
>   display: inline;
>
> }
>
> That should do the trick.

Thanks Marnen. I'll try that.

pepe

unread,
Oct 12, 2010, 5:54:20 PM10/12/10
to Ruby on Rails: Talk
> .field_with_errors {
>   display: inline;
>
> }

Just tried it and worked as expected. Thanks Marnen.
Reply all
Reply to author
Forward
0 new messages