Fortunately writing custom validation is really easy in Rails. In your model create a method called validate and in there you can place your custom validation checks. For example:
def validate
errors.add_to_base "If you are attaching a file you must enter a label for it" if !attachment.blank? and attachment_label.blank?
end
This custom validation does a simple check to see if the user has attempted to upload an attachment and if they have, ensure that they have entered a label for it.
The errors.add_to_base part means the error message will be output along with any other error messages using
<%= error_messages_for :object %>
If you require your custom validation to be reused across your more than one ActiveRecord model you should take a look at Peter Marklund's article that looks at all the different ways this can be achieved.
About Paul
Paul works for Kyan web design agency in Surrey, UK as a Ruby on Rails developer.
Follow Paul on Twitter
Email: paulsturgess [at] gmail.com
Comments...
Yuz, good stuff
Pimp at 11 Mar 08 at 11:42
thanx i was really getting anoyed by now. thanx again
Usman at 27 Jul 08 at 23:46
You also can add a message error by an especific attribute, like this:
errors.add :name, 'Taigo isnt a name' if self.name == 'Taigo'
Tiago Albineli Motta at 31 Jul 08 at 23:05
Thanks. Exactly what I needed. Found off Google. It was nice and concise.
Thanks at 18 Sep 08 at 20:41
<%= error_messages_for :object %><%= error_messages_for :object %><%= error_messages_for :object %><%= error_messages_for :object %><%= error_messages_for :object %><%= error_messages_for :object %>
<%= error_messages_for :object %> at 25 Feb 09 at 06:12
Yuz, good stuff
Pimp commented on 11 Mar 08 at 11:42 at 25 Feb 09 at 06:15
Ha, nice... Simple, clean. I forgot about it for sec, you where the first Google-hit ;)
Angelo Michel at 11 May 10 at 10:10
Thats how things should be ... simple
alexey at 31 Aug 10 at 04:09
Did not work for me. I created validator in a model, but go error on execution:
class Mypage < ActiveRecord::Base
def validate_test
if @mypage.disp_text =~ /@/
errors.add(:disp_test, "Yor text include @ sign -------")
@mypage.disp_text = "----changed ------"
end
end
validates_presence_of :disp_text, :background_picture, :played_sound
validate_test
end
An this is the error I got:
undefined local variable or method `validate_test' for #<class:0x3a30698>
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:1959:in `method_missing'
Zalek Bloom at 24 Oct 10 at 09:56
@Zalek
The proper syntax for calling your custom validation, in this case 'validate_test', is to call the active record method (or if you are in rails 3, active model) 'validates' and pass in the name of your custom validation method as a symbol such as:
class MyPage < ActiveRecord::Base
validates :validate_test
def validate_test
unless # Your test logic here
errors.add :your_attribute, "Error Message"
end
end
end
Mike at 02 Nov 10 at 19:01
nice post
batz at 02 Mar 11 at 07:43
@Mike: The validates method is only available in Rails 3.
Mark Wilden at 31 Mar 11 at 10:42
nice explanation
kousika at 20 Apr 11 at 00:24
Rails 3 method uses locales -- making your work neater and modularized.
Yasky at 26 Oct 11 at 10:38
You can read more about the locales file for error styling here on the Rails Guides I18n API #5. Here is a post I wrote up on how to harness the locales file in error styling: http://igbanam.wordpress.com/2011/10/25/customizing-error-messages-in-rails/
Yasky at 26 Oct 11 at 10:40
Got something to say?