Weird validation issue


#1

Hi,

I’m having a weird validation issue. Validation isn’t working for
certain attributes and, for some reason, it’s also affecting things
outside of validation.

For campers on certain camp types, we require some information about
their school. I tried to validate the information this way:

validates_presence_of :school_type, :if => Proc.new { |c|
!c.booking.nil? and ( c.booking.camp.camptype == Camptype.find(:first,
:conditions => [ ‘name = ?’, “Children’s Camp”]) or
c.booking.camp.camptype == Camptype.find(:first, :conditions => [
‘name = ?’, ‘Adventure Camp’]) ) }
validates_presence_of :school_year, :if => Proc.new { |c|
!c.booking.nil? and ( c.booking.camp.camptype == Camptype.find(:first,
:conditions => [ ‘name = ?’, “Children’s Camp”]) or
c.booking.camp.camptype == Camptype.find(:first, :conditions => [
‘name = ?’, ‘Adventure Camp’]) ) }
validates_presence_of :school_name, :if => Proc.new { |c|
!c.booking.nil? and ( c.booking.camp.camptype == Camptype.find(:first,
:conditions => [ ‘name = ?’, “Children’s Camp”]) or
c.booking.camp.camptype == Camptype.find(:first, :conditions => [
‘name = ?’, ‘Adventure Camp’]) ) }

but for some reason, that meant I could no longer add camps. (I wasn’t
getting a syntax error either).

I then changed the validation code to this:

if !self.booking.nil? and !self.booking.camp.nil? and (

self.booking.camp.camptype == Camptype.find(:first, :conditions => [
‘name = ?’, “Children’s Camp”]) or self.booking.camp.camptype ==
Camptype.find(:first, :conditions => [ ‘name = ?’, ‘Adventure Camp’])
)

  if self.school_type.blank?
    errors.add(:school_type, 'must be provided.')
  elsif !Camper::SCHOOL_TYPES.include?(self.school_type)
    errors.add(:school_type, 'is invalid.')
  end

  if self.school_year.blank?
    errors.add(:school_type, 'must be provided.')
  end

  if self.school_name.blank?
    errors.add(:school_type, 'must be provided.')
  end

end

but that had exactly the same effect.

Interestingly enough, it got through the code to the exact same point
as it would if it had actually added the camper to the booking.
Looking at the DB, the camper record was there but didn’t have
anything for contact_id or booking_id.

if !camper_already_attending && @contact.save && @camper.valid? &&

(@camper.contact = @contact) # (Yes, that last equal should
definitely be a single one!)
unless @booking.contactslist.include?( @contact.id )
# Need to add a role of “SU Camper” to the contact here
camper_role = Role.find(:first, :conditions => “name = ‘SU
Camper’”)
@contact.add_role(camper_role)
camper_added = false
particular_error = ‘’
if @booking.camp.camptype == Camptype.find(:first, :conditions
=> [ ‘name = ?’, ‘Parenting Camp’ ] )
if @booking.campers.size == 2
particular_error = ‘The booking is already full.’
else
@booking.campers << @camper
camper_added = true
end
else
@booking.campers << @camper
camper_added = true
end

    if @note.save
      @camper.add_note(@note)
    end

    if !camper_added
      @camper.destroy
      flash[:warning] = 'Camper could not be added to the booking:

’ + particular_error
redirect_to :action => ‘showbooking’, :id => @booking
elsif @camper.child? and !(@camper.booking.nil? or
@camper.booking.camp.camptype == Camptype.find(:first, :conditions =>
[ ‘name = ?’, ‘Parenting Camp’]) or @camper.booking.camp.camptype ==
Camptype.find(:first, :conditions => [ ‘name = ?’, ‘Family Camp’]) )
and @camper.guardian.nil?
flash[:notice] = ‘Camper record saved.’
redirect_to :action => ‘addguardian’, :id => @booking,
:camper => @camper
else

THIS IS WHERE IT GOT TO

      flash[:notice] = 'Camper was successfully added to the 

booking.’
if @booking.camp.within_age_range?(@camper)
redirect_to :action => ‘showbooking’, :id => @booking,
:highlight => @camper.id
else
redirect_to :action => ‘alert_outside_age’, :id =>
@camper.id
end
end

  else
    flash[:warning] = 'The camper ' + @contact.name + ' is already

on the booking.’
redirect_to :action => ‘showbooking’, :id => @booking
end
else
if camper_already_attending
flash[:warning] = ‘Sorry, that camper is already attending the
camp.’
redirect_to :action => ‘showbooking’, :id => @booking
else
render :action => ‘addcamper’, :layout =>
‘layouts/manualbooking’
end
end

If I remove the validation code, everything else works fine.

Any ideas? I’m stumped!

Dave


Site: http://antidis.com/


#2

Does anyone have any ideas?

Dave

On 2/8/06, David B. removed_email_address@domain.invalid wrote:

!c.booking.nil? and ( c.booking.camp.camptype == Camptype.find(:first,
:conditions => [ ‘name = ?’, “Children’s Camp”]) or
‘name = ?’, “Children’s Camp”]) or self.booking.camp.camptype ==
errors.add(:school_type, ‘must be provided.’)
Interestingly enough, it got through the code to the exact same point
@contact.add_role(camper_role)
else
flash[:warning] = 'Camper could not be added to the booking:
else
else
end
Site: http://antidis.com/


Site: http://antidis.com/