Ruby on Rails Wednesday, February 1, 2012

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.

No comments:

Post a Comment