Reputation: 10744
I have in model Invitation a field and attribute like:
field :recipients, :type => Array
I have an array with 4 emails in my controller like:
@invitation.recipients = ['', '', '', '']
I want validate in my model that each array's value match with a email regex sth like:
validates_format_of :recipients, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/u, :message => "is not a valid email address"
How can I validate regex of an array in mongoid?
Upvotes: 4
Views: 1372
Reputation: 23770
How about:
RE = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/u
validate :recipients_format
def recipients_format
unless recipients.all? { |r| r =~ RE }
errors[:recipients] = "are not all valid email addresses"
end
end
Upvotes: 6