简体   繁体   中英

validate array mongoid and ruby on rails

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM