Hi, in a form I insert a model''s values, I configm and in the controller I get @params["rigaFattura"] in this way: if @params["rigaFattura"]["id"] == nil rigaFattura = RigheFatture.new else rigaFattura RigheFatture.find(@params["rigaFattura"]["id"]) end rigaFattura.attributes = @params["rigaFattura"] if rigaFattura.save render_text rigaFattura.errors.inspect redirect_to(:action => "compila", :id => rigaFattura.fatture_id) else render_text rigaFattura.errors.inspect end In the model righe_fatture: class RigheFatture < ActiveRecord::Base set_table_name :righe_fatture belongs_to :fatture def validate errors.add("quantita", "has invalid format") unless quantita.integer? end end because I want to raise an error is the variable quantita is not an integer. And I was assuming to see the render_text anyway but... when I confirm the form I get a DB error: ActiveRecord::StatementInvalid in Fatture#aggiungiRiga ERROR: invalid input syntax for integer: "u" : INSERT INTO righe_fatture ("prezzounitario", "quantita", "descrizione", "riferimentobolla", "fatture_id", "tipo") VALUES(''9'', ''u'', ''u'', ''u'', ''2'', ''M'') script/server:51 Request Parameters: {:action=>"aggiungiRiga", :controller=>"fatture", :rigaFattura=>{"prezzounitario"=>"9", "quantita"=>"u", "descrizione"=>"u", "riferimentobolla"=>"u", "fatture_id"=>"2"}} Show session dump --- righePFatt: Response Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"} Show template parameters Someone have suggestions? What am I missing? Enrico -- "The only thing necessary for the triumph of evil is for good men to do nothing" Edmund Burke
I think it has to do with the database you''re using and when the type cast occurs for validations. The @param hash contains strings, and when you type cast a string using to_i in ruby you get 0 (zero). Try it out with irb: irb(main):011:0> value = ''u'' => "u" irb(main):012:0> value.to_i => 0 irb(main):013:0> value.to_i.integer? => true irb(main):014:0> value.integer? NoMethodError: undefined method `integer?'' for "u":String from (irb):14 from :0 irb(main):015:0> I think (this is just what I think, it may or may not be correct) that the validations does a type cast on the string parameter, then validates it. The result is that "u" passes the integer? check, but gets inserted as "u" into an integer field. As a side note, I''m using mysql 4.1.9 and I couldn''t get this to fail. The "u" was inserted as a 0 into an integer field. Mysql (being so rock solid for data validation) just put 0 in. An alternate solution - use a regular expression. Validate like this: errors.add("quantita", "has invalid format") unless quantita =~ /\d*/ Enrico Teotti wrote:> Hi, > in a form I insert a model''s values, I configm and in the controller I > get @params["rigaFattura"] in this way: > > if @params["rigaFattura"]["id"] == nil > rigaFattura = RigheFatture.new > else > rigaFattura > RigheFatture.find(@params["rigaFattura"]["id"]) > end > > rigaFattura.attributes = @params["rigaFattura"] > if rigaFattura.save > render_text rigaFattura.errors.inspect > redirect_to(:action => "compila", :id => > rigaFattura.fatture_id) > else > render_text rigaFattura.errors.inspect > end > > In the model righe_fatture: > > class RigheFatture < ActiveRecord::Base > set_table_name :righe_fatture > belongs_to :fatture > > def validate > errors.add("quantita", "has invalid format") unless > quantita.integer? > end > > end > > because I want to raise an error is the variable quantita is not an > integer. And I was assuming to see the render_text anyway but... when > I confirm the form I get a DB error: > > ActiveRecord::StatementInvalid in Fatture#aggiungiRiga > > ERROR: invalid input syntax for integer: "u" : INSERT INTO > righe_fatture ("prezzounitario", "quantita", "descrizione", > "riferimentobolla", "fatture_id", "tipo") VALUES(''9'', ''u'', ''u'', ''u'', > ''2'', ''M'') > > script/server:51 > Request > > Parameters: {:action=>"aggiungiRiga", :controller=>"fatture", > :rigaFattura=>{"prezzounitario"=>"9", "quantita"=>"u", > "descrizione"=>"u", "riferimentobolla"=>"u", "fatture_id"=>"2"}} > > Show session dump > > --- > righePFatt: > > Response > Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"} > > Show template parameters > > Someone have suggestions? What am I missing? > Enrico >
Enrico Teotti wrote:>because I want to raise an error is the variable quantita is not an >integer. And I was assuming to see the render_text anyway but... when >I confirm the form I get a DB error: > >have a look at the validates_numericality_of validation that does exactly what you are looking for. If you want to perform validation for yourself, you can access the attribute''s original value (before being type-casted by rails) via the attributename_before_type_cast attribute. Sebastian
I''ve already tried validates_numericality_of but I''ve get this error when I try to access: ScriptError in <controller not set>#<action not set> undefined method `validates_numericality_of'' for RigheFatture:Class the model is: class RigheFatture < ActiveRecord::Base set_table_name :righe_fatture belongs_to :fatture validates_numericality_of :quantita end Using a reg exp on the attribute give me the same result as def validate errors.add("quantita", "has invalid format") unless quantita.integer? end but using before_type_cast in the validate, it works! this is the exact syntax: errors.add("quantita", "You MUST insert a number.") unless quantita_before_type_cast =~ /^[0-9]+/ thanks for the suggestions! Enrico ps I prefer to get that error then to see a 0 in my column... I don''t understand why if a values can''t be converted to integer, the method to_i returns 0... and if the user inputs ''0'' ? ... -- "The only thing necessary for the triumph of evil is for good men to do nothing" Edmund Burke
Enrico Teotti wrote:>I''ve already tried validates_numericality_of but I''ve get this error >when I try to access: > >ScriptError in <controller not set>#<action not set> > >undefined method `validates_numericality_of'' for RigheFatture:Class > > >hm, what version of rails are you using? validates_numericality_of was added in a recent update. Sebastian
mmmm rails -v 1.8.2 I assume I need to update :-) On Sat, 26 Mar 2005 12:14:41 +0100, Sebastian Kanthak <sebastian.kanthak-ZS8b95Whz3sUSW6y5lq3GQ@public.gmane.org> wrote:> Enrico Teotti wrote: > > >I''ve already tried validates_numericality_of but I''ve get this error > >when I try to access: > > > >ScriptError in <controller not set>#<action not set> > > > >undefined method `validates_numericality_of'' for RigheFatture:Class > > > > > > > hm, what version of rails are you using? validates_numericality_of was > added in a recent update. > > Sebastian > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- "The only thing necessary for the triumph of evil is for good men to do nothing" Edmund Burke