Ruby on Rails
Wednesday, February 1, 2012
thanks for reply but its unusable bcz value gets blank before validation.
example.
u = User.new
u.birth_date = '31/31/2011'
u.birth_date #=> nil
u.valid? #=> true
u.birth_date = '1/1/2011'
u.birth_date #=> Sat, 01 Jan 2011
On Wed, Feb 1, 2012 at 2:21 PM, Neethu Satheesh <lists@ruby-forum.com> wrote:
Hi Sandip,
You can provide model level validation for the date as follows.
class User < ActiveRecord::Base
validate :validate_birth_date
def initialize(args={})
super
end
def validate_birth_date
valid_date_format = /<write a regex for the date in the
appropriate format>/
if !birth_date.blank? && !birth_date.match(valid_date_format)
errors.add(:base, "Birth date is invalid")
end
end
end
You can explore more about errors in
http://guides.rubyonrails.org/active_record_validations_callbacks.html#working_with_validation_errors-errors
Thanks,
Neethu
--
Posted via http://www.ruby-forum.com/.
--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
--
sαη∂ιρ Rαηѕιηg
---
www.funonrails.com
twitter, github @sandipransing
skype sandip.ransing
--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment