Parsing validation on model


#1

hey,
i want to have the validation of a model being parsed in to a string and
added
to a global string. I read an csv file, do validation, and create an
error
message for each record, and add that to the global error message to
display it
later to the user

i used this code, but i dont get it to work

@msg = “”

looping…

error = false
errormsg = “”

employee = Employee.find(:all, :conditions =>['firm_id = ? and
first_name = ?
and last_name = ? ', firm_id, first_name, last_name])
if employee.empty?
error = false
else
error = true #name already exist
errormsg = “Firstname and lastname are already taken.”
end

if error
@msg += "Record: " + first_name + ", " + last_name + ", " + phone + ",
" +
phone_pin + ", " + errormsg + “

else
@tempemployee= Employee.new
#SETING THE VALUES TO THE OBJECT

#SAVING THE OBJECT
if @tempemployee.save
else
err = parse_errors(‘employee’)
@msg += "Record: "“saving error” + err + “

end
end


the method i use

def parse_errors(obj, strip=false)
return nil if obj.nil?
err = error_messages_for(obj)
err[/(.)

There were problems with the following
fields:</p>(.

)</div>/] if !err.nil?
return “” if $2.nil?
str = “” + $2.to_s + “
str.gsub!(/

    /,’’);
    str.gsub!(/</ul>/,’’);
    str.gsub!(/’’/, “\\’”) unless strip==true
    return str
    end

    the error i get

    undefined method `error_messages_for’ for
    #EmployeesController:0x3778838

    i need this to work in the controller, not in the rhtml, cuz it is in a
    loop.

    thanks in advance
    Nick


#2

i found a solution,

put this in your application.rb

def parse_errors(obj)
return nil if obj.nil?

str = "<ul>"
obj.errors.each_full do |msg|
str += "<li>" + msg +"</li>"
end
str += "</ul>"
return str

end

to call the method just do

err = parse_errors(@object)