#39 Customize Field Error
Jun 01, 2007 | 3 minutes |
Views
When a validation error occurs, Rails helpfully wraps the field in a div tag so you can style it. But sometimes you don't want this behavior. In this episode you will see how to customize it.
- Download:
- mp4Full Size H.264 Video (8.86 MB)
- m4vSmaller H.264 Video (5.4 MB)
- webmFull Size VP8 Video (12.2 MB)
- ogvFull Size Theora Video (12.7 MB)
Hi Ryan, thanks for this (and all) screencast. About this one, where can we find the reference of ActionView? I think this documentation will be usefull for rewrite the functions.
Regards.
Already used that :).
"<span class=\"fieldWithErrors\">#{html_tag}</span>"
Thanks anyway.
Hmm... What if I don't want another wrapping tag but just want to set the class attribute of the input field, then how would I do?
Nice job!
I know you get tons of requests for screencasts but I'd actually like to see something small about Rake. Obviously you couldn't cover all of Rake in 3-4 minutes, but it might be instructive to see a small task or two that you've written that find yourself using frequently.
Thanks here too. I"ve wasted a lot of time trying to make my forms look good when errors are displayed, this allows me to get rid of a bunch of hacky code.
These RailsCasts are so great - I am speechless! A BIG THANK YOU!
Greeting from Germany,
Martin
@Martin, I don't know if this feature is documented in the rails api. I haven't seen it anyway.
@Erik, there's a snippet on this page which does that:
http://snippets.dzone.com/tag/field_error_proc
I think it would be cleaner to change an option in instance_tag and have it regenerate the HTML. You just have to watch out for the seamless loop because regenerating the HTML calls the proc again.
@Jim, good suggestion. An episode about Rake is planned, just need to sit down and record it some day. :)
Ryan,
You rock. I have been a big fan of yours through both your amazing posts at railsforum.com and these railscasts.
Keep up the good work. All the best.
Usefull tutorial. I haven't commented any screencasts on this page before but I've seen all of them - thanks! You're doing great job for rails community :)
I am just wondering if there is a way to wrap the error that corresponds to the input control within the span block. It is always nicer to allow the user to see the error message right beside the input control rather than a summary of all the errors at the top of the page.
Thanks for the videos, I always look forward to them.
@Chris, I beleive this is possible through instance_tag. You can access the model through that in addition to the errors and the name of the attribute the field represents. The pieces are all there, you just have to figure out how to put them together.
Unfortunately instance_tag (ActionView::Helpers::InstanceTag) is poorly documented. You have to read the source code.
Hi Ryan, thanks for this new screencast. About this error validation tips, I have one question. Sometimes when I manualy add an error action in a validation method in the model, nothing happend on the field.
IE:
def validate
if a_special_check
errors.add("field_name", "my message...")
return false
end
Any idea ?
Hmm, if the field name you provide matches the name used in the field itself, it should work. You might want to try making it a symbol.
errors.add(:field_name, ...)
Thanks Ryan. I wanted to learn how to do that just a few days ago and this railscast will save me a lot of time!
I'm a big fan and would not miss an episode! Keep up the great job.
It would be great to show an example of how to mark an associated label with an asterisk or something similar. I think this is used a lot to mark a field as "required" i.e. instead of having a red border around the field, you would mark the label shown above the field either in read or with an red asterisk.
How would I do that?
Hi Ryan,
As you tell me, I try to use :field_name instead of "field_name", but dont work... Moreover, this feature dont work only with id fields. Fo you have another idea ?
Thanks
@Olivier, hmm I'm not sure what the problem is without seeing more code. But here is not the best place to post the code. Instead I recommend starting a thread over at railsforum.com with a detailed explenation of your problem. I frequently visit that forum so I'll probably be able to reply there. Thanks. :)
I have always wanted to know this. Extremely helpful!Thanks also for your contributions over at railsforum.com!
It seems to me, that in rails 2.0.2 the code doesn't work. Restarting my mongrel server I get several errors ...
My fault, the code has to be at the very end of environment.rb -- sorry
Thank you, exactly what I need! I love your screencasts. Good job.
Thanks.
Instead I recommend starting a thread over at railsforum.com with a detailed explenation of your problem. I
This is excellent as always! Thank you so much. One thing though: Doing this seems to break my "rake routes" with an "uninitialized constant ActionView". I'm still pretty green with rails so I don't know the answer to this myself or really where to look yet. Do you have a fix for this? (For right now I just comment my block out and run rake routes)
Thanks so much!
is a excellente tutorial , i have one question , if my form is remote, I can do the same? or that would be the method
In this case I'm using qtip2 + jquery.validation plugin.
http://craigsworks.com/projects/qtip2/demos/validation
http://docs.jquery.com/Plugins/validation
Groovy!
This episode has been updated for Rails 5 as a blog post. Customize Field Error in Rails 5