'Mail_form gem not showing error for invalid email

I have working mail_form contact form with google's smtp service. I satisfied almost with everything, but form not showing errors for invalid email. There is an error appears if email field is blank, but if something present in the field and you press "Send message" button nothing happens - validation not allowing send message, but user not informed that something wrong.

contact.rb

class Contact < MailForm::Base
  attribute :name
  attribute :email, validate: URI::MailTo::EMAIL_REGEXP
  attribute :message, validate: true
  attribute :file, attachment: true
  
  def headers
    { 
      subject: "My Contact Form",
      to: '[email protected]',
      from: %("#{name}" <#{email}>)
    }
  end
end

views/contacts/new.html.haml

= form_with model: @contact do |f|
  = f.label :name
  = f.text_field  :name, required: true, class: "text-field"
  = f.label :email
  = f.text_field :email, required: true, class: "text-field", placeholder: "[email protected]"
  = f.label :message
  = f.text_area :message, rows: 6, required: true, class: "text-field"
  = f.label :files, class: "inline-block p-1 pt-2"
  = f.file_field :file, class: "block p-1 font-body text-sm"

  = f.submit t('send_message'), class: "submit"

contacts_controller.rb

class ContactsController < ApplicationController

  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(contact_params)
    @contact.request = request
    if @contact.deliver
      redirect_to contacts_path, notice: 'Message sent!'
    else
      flash.now[:error] = 'Could not send message'
      render :new
    end
  end

  private
    def contact_params
      params.require(:contact).permit(:name, :email, :message, :file)
    end
end

I have tried many things to show error messages, which have worked in my previous projects, but can't make it work with mail_form contact form.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source