I have a form where i'm trying to validate that a field called "birthday" is not blank, and that the date is a valid date format that the Chronic gem can parse. I always get the error message "Birthday is invalid". I've been trying a simple format "10/10/2010".
How can i validate that the birthday field is of a format that chronic can parse?
User.rb model
class User < ActiveRecord::Base
validates :birthday, :presence => true
validate :birthday_is_date
validate :position, :presence => true
# validate the birthday format
def birthday_is_date
errors.add(:birthday ,Chronic.parse(birthday)) # testing to see the value of :birthday
errors.add(:birthday, "is invalid test message") if ((Chronic.parse(:birthday) rescue ArgumentError) == ArgumentError)
end
end
contacts_controller.rb
# POST /contacts/1/edit
# actually updates the users data
def update_user
@userProfile = User.find(params[:id])
respond_to do |format|
if @userProfile.update_attributes(params[:user])
format.html {
flash[:success] = "Information updated successfully"
redirect_to(profile_path)
}
else
format.html {
flash[:error] = resource.errors.full_messages
render :edit
}
end
end
end