Hi,
What I’m asking might be simple for some of you but I’m trying to fix
this for two days and I just can’t figure out how to make this thing
work. I have a model with a date_select validation that is working
just fine except when I fill the form with a invalid date like Feb 30
1989. I thought the plugin validates_multiparameter_assignments would
catch this kind of invalid date but for some reason is just not
working. Below I’m pasting my model and controller code to see if
someone can help me.
class Spec < ActiveRecord::Base
belongs_to :user
ALL_FIELDS = %w(first_name last_name address phone gender birthdate
city state zip_code)
STRING_FIELDS = %w(first_name last_name address phone city state)
VALID_GENDERS = [“Male”, “Female”]
START_YEAR = 1900
END_YEAR = Date.today.year - 18
ZIP_CODE_LENGTH = 10
validates_length_of STRING_FIELDS, :maximum => DB_STRING_MAX_LENGTH
validates_inclusion_of :gender, :in => VALID_GENDERS, :allow_nil =>
true, :message => “must be male or female”
validates_presence_of :zip_code, :birthdate
validates_multiparameter_assignments :message => " is not a valid
date."
validates_each :birthdate do |record, attr, value|
record.errors.add attr, “is not a valid date. You must be at
least 18 years old to sign in.” if value > Date.new((Date.today.year -
18),(Date.today.month),(Date.today.day))
end
end
class SpecController < ApplicationController
before_filter :protect
Edit the user’s spec.
def edit
@user = User.find(session[:user_id])
@title = “aykal - edit #{@user.screen_name} profile”
@user.spec ||= Spec.new
@spec = @user.spec
if param_posted?(:spec)
@user.spec.valid?
if @user.spec.update_attributes(params[:spec])
flash[:notice] = “Changes saved.”
redirect_to :controller => “user”, :action => “index”
end
end
end
end
Thanks,
Thiago G.