Search code examples
ruby-on-railsrubyemailemail-validation

What's the best way to validate multiple emails and handle errors in Rails?


In the current app I'm building I've got a textarea where a user will enter a comma-delimited list of email addresses.

I'm currently splitting the list into an array and then saving one by one. But if, say, I have this input...

[email protected], test@example, [email protected]

... then [email protected] will be saved, but saving test@example will fail. So I then need to remove [email protected] from the comma-delimited string of values that I pass back to the textarea when I show the error that test@example isn't a valid email address.

Is there a better way to validate these on the server side and handle errors without getting fancy / ugly in the controller?

Thanks in Advance!


Solution

  • Assuming this is a model that has_many emails, and the email model uses :validate_email, you could do something like the following:

    class Foo < ActiveRecord::Base
      validate :must_not_have_invalid_addresses
    
      ...
    
      def emails=(addresses)
        @invalid_addresses = []
        addresses.split(",").each do |address|
          @invalid_addresses.push(address) unless emails.create({:address => address})
        end
      end
    
      def must_not_have_invalid_addresses
        errors.add_to_base("Some email addresses were invalid") unless @invalid_addresses.empty?
      end
    
    end
    

    This provides a validation error + an array of the invalid email addresses which you can make accessible to your view if you like.