Joshua Muheim
2007-Oct-17 08:45 UTC
send("#{attr_name}_before_type_cast").nil? -- does not work!
Hi all I''m writing a validates_url_format_of validation helper. So far everything works good, but the 11th line does not work! --- ActiveRecord::Errors::default_error_messages[:invalid_url_protocol] ''must be using the HTTP or HTTPS protocoll'' ActiveRecord::Errors::default_error_messages[:invalid_url_format] = ''has an incorrect link format'' module ActiveRecord::Validations::ClassMethods def validates_url_format_of(*attr_names) configuration = {} configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) validates_each(attr_names.first.to_s) do |record, attr_name, value| unless configuration[:if] and not evaluate_condition(configuration[:if], record) # Evaluate :if option if record.send("#{attr_name}_before_type_cast").nil? # Is the URL attribute empty? next if configuration[:allow_nil] # Skip this one if emptyness is allowed record.send("#{attr_name}=", ''http://'' + record.send(attr_name.to_sym)) if record.send(attr_name) !~ /^[a-z]+:\/\// # Add the default protocoll HTTP to the URL if none specified end if record.send(attr_name.to_sym) !~ /^(http|https):\/\//i # Only allow HTTP and HTTPS URL''s record.errors.add(attr_name, ActiveRecord::Errors::default_error_messages[:invalid_url_protocol]) elsif record.send(attr_name.to_sym) !~ /^(http|https):\/\/([a-z0-9\-äöüèàéê\.]+)(\.[a-z]{2,})(\/(.*))?$/i # Check if it is a basic valid URL record.errors.add(attr_name, ActiveRecord::Errors::default_error_messages[:invalid_url_format]) end end end end end --- I copied it from another Rails validation helper, so I don''t get it why if record.send("#{attr_name}_before_type_cast").nil? # Is the URL attribute empty? does not work... :-/ Thanks for help. Josh -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---