Answer the question
In order to leave comments, you need to log in
How to check email validation on the backend while making the email input field optional?
It is necessary to check email validation on the backend, while in the view the input field must be optional.
Answer the question
In order to leave comments, you need to log in
Solution found: in the model I wrote something like the following
class Contact < MailForm::Base
attribute :phone
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i,
presence: false, allow_blank: true
validate :at_least_a_contact
def headers
{
:subject => "My Contact Form",
:to => "[email protected]",
:from => %("#{phone}" <#{email}>)
}
end
private
def at_least_a_contact
unless phone.present? || email.present?
errors.add(:contact, "You need at least a contact method")
end
end
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question