Errors.add don't work

Hi RoR Comnunity!

I made two virtual attributes to enter date into a text_field.

Here are the tbl_bank.entries:

date text_field gueltig_ab

def gueltig_ab_string
if gueltig_ab
gueltig_ab.strftime(’%d.%m.%Y’)
end
end

def gueltig_ab_string=(gueltig_ab_str)
if gueltig_ab_str != ‘’
self.gueltig_ab = Date.strptime(gueltig_ab_str, ‘%d.%m.%Y’)
else
self.gueltig_ab = gueltig_ab_str
end

rescue ArgumentError
  @gueltig_ab_invalid = true

end

def validate
errors.add(:gueltig_ab, “Datumsformat muss TT.MM.JJJJ sein”) if
@gueltig_ab_invalid
end

date text_field gueltig_bis

def gueltig_bis_string
if gueltig_bis
gueltig_bis.strftime(’%d.%m.%Y’)
end
end

def gueltig_bis_string=(gueltig_bis_str)
if gueltig_bis_str != ‘’
self.gueltig_bis = Date.strptime(gueltig_bis_str, ‘%d.%m.%Y’)
else
self.gueltig_bis = gueltig_bis_str
end

rescue ArgumentError
@gueltig_bis_invalid = true
end

def validate
errors.add(:gueltig_bis, “Datumsformat muss TT.MM.JJJJ sein”) if
@gueltig_bis_invalid
end

The Problem is, that the second errors:add(:gueltig_bis) works but the
first errors.add(:gueltig_ab) don’t work.

Do somebody knows why?

Thanks ahead for every proposal!

Regs,

Hermann

I found a solution.

My fault was, that there where two validations with the same name -->
“def validate”.

In found in the rails api the solution:

I have only to rename the validation blocks an then trigger the
validations with

" validate :block_name"

Here are my new tbl_banks.rb entries:

validate :valid_gueltig_ab
validate :valid_gueltig_bis

Date as textfield with virtual attributes

date text_field gueltig_ab

def gueltig_ab_string
if gueltig_ab
gueltig_ab.strftime(’%d.%m.%Y’)
end
end

def gueltig_ab_string=(gueltig_ab_str)
if gueltig_ab_str != ‘’
self.gueltig_ab = Date.strptime(gueltig_ab_str, ‘%d.%m.%Y’)
else
self.gueltig_ab = gueltig_ab_str
end

rescue ArgumentError
  @gueltig_ab_invalid = true

end

def valid_gueltig_ab
errors.add(:gueltig_ab, “Datumsformat muss TT.MM.JJJJ sein”) if
@gueltig_ab_invalid
end

date text_field gueltig_bis

def gueltig_bis_string
if gueltig_bis
gueltig_bis.strftime(’%d.%m.%Y’)
end
end

def gueltig_bis_string=(gueltig_bis_str)
if gueltig_bis_str != ‘’
self.gueltig_bis = Date.strptime(gueltig_bis_str, ‘%d.%m.%Y’)
else
self.gueltig_bis = gueltig_bis_str
end

rescue ArgumentError
@gueltig_bis_invalid = true
end

def valid_gueltig_bis
errors.add(:gueltig_bis, “Datumsformat muss TT.MM.JJJJ sein”) if
@gueltig_bis_invalid
end

end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs