#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
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!!!!
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.
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/
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>
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!
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
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.
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?
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 theEmailFormatValidator
toEmailValidator
. 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
i'm getting trouble with the partial.
undefined local variable or method `target' for #<#Class:0x00000003f702d0:0x00000002599460>
the form:
The partial have the same code...
but if this code is directly in the form it's working...
but i want to work with the partial...
Hi folks
what if in the form we are setting remote = true then none of this will work because we are using ajax and preventing the form from refreshing? If you have any idea please hit the reply button.
with thx
Ben
Our reproducing chart (with images) below will lead and also assist you in getting your very own unique beasts.