Commit caa8090addbabb07f9287b90fa0f85e791cb7ed6
1 parent
8e1cd3f7
Exists in
master
and in
22 other branches
rails3: fix birthday validation
Showing
2 changed files
with
5 additions
and
6 deletions
Show diff stats
app/models/person.rb
| 1 | # A person is the profile of an user holding all relationships with the rest of the system | 1 | # A person is the profile of an user holding all relationships with the rest of the system |
| 2 | class Person < Profile | 2 | class Person < Profile |
| 3 | 3 | ||
| 4 | - attr_accessible :organization, :contact_information, :sex | 4 | + attr_accessible :organization, :contact_information, :sex, :birth_date |
| 5 | 5 | ||
| 6 | SEARCH_FILTERS += %w[ | 6 | SEARCH_FILTERS += %w[ |
| 7 | more_popular | 7 | more_popular |
| @@ -159,9 +159,8 @@ class Person < Profile | @@ -159,9 +159,8 @@ class Person < Profile | ||
| 159 | validates_multiparameter_assignments | 159 | validates_multiparameter_assignments |
| 160 | 160 | ||
| 161 | validates_each :birth_date do |record,attr,value| | 161 | validates_each :birth_date do |record,attr,value| |
| 162 | - if value && value.year == 1 | ||
| 163 | - record.errors.add(attr) | ||
| 164 | - end | 162 | + date_str = record.birth_date_before_type_cast |
| 163 | + record.errors.add(attr) if value.blank? && !date_str.blank? | ||
| 165 | end | 164 | end |
| 166 | 165 | ||
| 167 | def self.fields | 166 | def self.fields |
test/unit/person_test.rb
| @@ -1168,14 +1168,14 @@ class PersonTest < ActiveSupport::TestCase | @@ -1168,14 +1168,14 @@ class PersonTest < ActiveSupport::TestCase | ||
| 1168 | should 'handle multiparameter attribute exception on birth date field' do | 1168 | should 'handle multiparameter attribute exception on birth date field' do |
| 1169 | assert_nothing_raised ActiveRecord::MultiparameterAssignmentErrors do | 1169 | assert_nothing_raised ActiveRecord::MultiparameterAssignmentErrors do |
| 1170 | p = Person.new( | 1170 | p = Person.new( |
| 1171 | - :name => 'birthday', :identifier => 'birthday', :user_id => 999, | 1171 | + :name => 'birthday', :identifier => 'birthday', |
| 1172 | 'birth_date(1i)' => '', 'birth_date(2i)' => '6', 'birth_date(3i)' => '16' | 1172 | 'birth_date(1i)' => '', 'birth_date(2i)' => '6', 'birth_date(3i)' => '16' |
| 1173 | ) | 1173 | ) |
| 1174 | end | 1174 | end |
| 1175 | end | 1175 | end |
| 1176 | 1176 | ||
| 1177 | should 'not accept an empty year on birth date' do | 1177 | should 'not accept an empty year on birth date' do |
| 1178 | - p = Person.new({"birth_date(2i)"=>"11", "birth_date(3i)"=>"15", "birth_date(1i)"=>""}) | 1178 | + p = Person.new(:birth_date => "1115") |
| 1179 | p.valid? | 1179 | p.valid? |
| 1180 | assert p.errors[:birth_date.to_s].present? | 1180 | assert p.errors[:birth_date.to_s].present? |
| 1181 | end | 1181 | end |