#211 Validations in Rails 3
Rails 3 offers several new additions to validations. Here learn how to make a custom error_messages partial, reflect on validations, and clean up complex validations in a model.
- Download:
- source code
- mp4
- m4v
- webm
- ogv



Awesome, always looking forward to the Mondays railscast, even if it was late today :)
Thanks, Awesome as allways :)
All new projects in ubernetics will be developed in Rails 3, all this rails 3 series railscasts help me to take this decision.. thanks Ryan!! Rails 3 here we go!!!!
This video was really glitchy for me -- but all of the past videos have been fine. There were many parts where the frame wouldn't completely be painted when transitioning to a new screen. For example, when switching from the console to the browser, I would still see the console, but could see the cursor kind of "painting" the web browser in. Hard to explain. I think it's encoded poorly. Have you tried using xvid?
To ammend the above, this problem was only when using VLC player. But as I said before, I have never had this problem with any of your other videos.
@ryeguy, I'm experimenting with various video codec settings. Were you watching the full resolution or the iPod/AppleTV one?
same here on the glitchy video. Content is great - but for the first time - the video doesn't update when you've switched views (from textmate to safari for example) for 5 - 10 seconds and is pixelated too.
Thanks Ryan.
@dpf, are you using VLC player?
What would we do without you?
Over the last few episodes I have been convinced that I should be heading in the RoR 3 direction sooner rather than later.
A big 'thank you' Ryan.
@ryeguy is right - same experience here - the video is fine in totem - but messed up in VLC. Perhaps this isn't worth addressing given the small population of us it effects.
Thanks for the summary Ryan. I was the author of "Sexy Validations", you can see my original writeup here: http://thelucid.com/2010/01/08/sexy-validation-in-edge-rails-rails-3/
Ryan - the problem was with the full version of the video. Didn't try the iphone/pod version.
Here is my simple RoR plugin for custom error template:
http://github.com/kelso/render_errors
If mail is not formatted properly, then the asterisks '*' is rendered between two div's.
Is there a simple way to move '*' into the first div?
<div class="fieldWithErrors"><label for="user_email">Email</label></div>*<br />
<div class="fieldWithErrors"><input id="user_email" name="user[email]" size="30" type="text" value="x" /></div>
Great Ep as always Ryan Thanks!
I am on Win7 64bit, VLC does have a problem rendering. Media Player Classic has no problem. And since WMP uses the same codec as does MPC, it plays it fine, as well. Just VLC then.
Regards
Hey rbates, thank you for your useful and awesome casts! Ryan, PLEASE, can you "Build an entire application over a series of screencasts" with Rails 3, where you can cover all other sub-topics like this validation and other devise and etc. Please, do not neglect this comment and make me and people who want to see you build entire apps happy. I would even pay to watch those. Thanks!
FYI Ryan: I too have the same issue with the full version using VLC
Video works fine for me on VLC 10.6, Snow Leopard 10.6.3 (as well as in QuickTime)
er, i meant VLC 10.5
Works under OSX 10.5 with movist.
Works under linux with kaffeine qt4, it doesn't work with smplayer or mplayer.
Hey Ryan,
thanks a lot for this episode.
Some time ago I searched for a clever solution and ended up with formtastic in combination with validation reflection. Would you prefer formtastic over this more barebone approach with less abstraction?
For me it is important to get going fast and to have maintainable code I can adjust if I need to.
Kind regards
Bijan
Thanks again for the new episode.
Ruby-3 seems really wonderful
this is the first time video file doesn't play nice. i mean, there were freezed frames all over the cast.
don't know if it is my linux machine (ubuntu 9.10) or the player (vlc).
so, i want to know if you (Ryan) made it in a different way (different kind of codec may be). that way, i could see other alternatives.
thanks.
@Pauolo
You mean Rails 3?
Aaaaawesome!
Having classes of validations is really a step forward.
Nice cast. But one thing that struck me. How do you translate the those validation messages when you use the self made partial as shown in this cast?
David Heinemeier Hansson
The custom validator used here to validate an email address does not work in the just released rc of rails3:
Unknown validator: 'email_format'
So stuff in RAILS_ROOT/lib does nto get loaded I suppose...
Anybody know how to solve this?
Well should have done my research properly...
The custom validators have to go here:
app/validators
Cheers J
Thanks for posting this; saved me the trouble of doing research. :-)
Good post.
But
<%= render :partial => 'shared/error_messages', :target => @user %> does not work for me.
I have "undefined local variable or method `target'" error
So I use
<%= render :partial => 'shared/error_messages', :locals => {:target => @user} %>
Hi Ryan,
Thanks for the introduction to validation reflections!
Do you plan on showing how to implement custom FormBuilders?
Thanks Ryan.
@Olivier
1) Create new Helper
class Buildr < ActionView::Helpers::FormBuilder
def label(method, text = nil, options = {}, &block)
str = @template.label(@object_name, method, text, objectify_options(options), &block)
str = str + "*" if object.class.validators_on(method).map(&:class).include? ActiveModel::Validations::PresenceValidator
return str
end
end
2) In form_for call :builder => Buildr
Hii,
First, this cast is really good!
but, i have a problem with the validator:
---
ArgumentError in UsersController#index
Unknown validator: 'email_format'
---
Do i need include in controller?
this is the path of class: /lib/email_format_validator.rb
thanks
@Richard Leh
New release of rails 3 do not load automatically the .rb files in lib folder. But you can by putting this in your config/application.rb file:
config.autoload_paths += %W(#{Rails.root}/lib)
Reload your server.
thank you very much Ryan! Everything is always so clear!
In _error_messages can get the name of the resource which you couldn't save in this way:
... prohibited this <%= target.class.to_s %> from being saved
thanks again!
It should be mentioned that for the custom validation to work, the lib directory must be loaded with
config.autoload_paths += %W[#{config.root}/lib/]in application.rb.Awesome. Thank you!
Exactly what I was looking for, easy to understand, and fast to watch!
Thanks so much for this. I didn't watch the video but used the ASCIIcast. Precisely what I needed!
I followed this blog entry to load custom validators from
lib/validators. I also named theEmailFormatValidatortoEmailValidator. It makes more sense to me.:email => true. Great screencast as always, just needs the little update about loading validators. :)Here's the snippet:
config/application.rb
Agree with OP.
lib isn't autoloaded anymore.
Also, the blog post mentioned by tuscanidream doesn't explain how to get lib in engine to autoload. I think it would be good to autoload stuff from eninges, that don't belong in app. Or, make something like app/validators, which makes more sense to me.
Cheers
Ok, tried different things, but in the end I made an app/validators in my engine, and since all in app is (auto?) loaded, the validators from the engine are now loaded too.
Hmm.
You can also use this following line to make the error messages more personal..
And if you are unhappy with your model name you can always add an i18n translation to the model name!
Oops i ment target.class.name
First sign in through GitHub to post a comment.