Check massive mangoids and rubies on rails

In my Invitation model, the field and attribute are like:

field :recipients, :type => Array 

I have an array with 4 messages in my controller, for example:

 @invitation.recipients = ['', '', '', ''] 

I want my model to verify that each array value matches an email regular expression, for example:

 validates_format_of :recipients, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[az]{2,})$/u, :message => "is not a valid email address" 

How can I check array regex in mongoid?

+4
source share
1 answer

What about:

 RE = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[az]{2,})$/u validate :recipients_format def recipients_format unless recipients.all? { |r| r =~ RE } errors[:recipients] = "are not all valid email addresses" end end 
+6
source

Source: https://habr.com/ru/post/1399355/


All Articles