我有一个简单的表单,它使用的是模型(没有数据库表,但是我认为那没有关系),而且我似乎无法通过验证来进行测试:
表格:
= simple_form_for(@client_email, url: { action: "update" }, html: {class: "search_form", method: :put }) do |f|
= f.error_notification
.input-row
= f.input :oldDomain, :label => "Old Domain Name", input_html: {name: 'search_term'}, :required => true
= f.input :newDomain, :label => "New domain name", :required => true
[还有一个提交按钮,等等]
(请注意,这似乎也允许在不输入任何字段的情况下进行提交)
在我的模型中,我有:
class ClientEmail
include Virtus.model
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attribute :newDomain, String
attribute :oldDomain, String
validates :newDomain, :format => { :with => /\A([^@\s]+\.)+[^@\s]+\z/}
validates :oldDomain, :format => { :with => /\A([^@\s]+\.)+[^@\s]+\z/}
validate :cannot_be_present
def cannot_be_present
newDomClients = Client.where("email like :foo", {:foo => "%#{newDomain}%"})
if newDomClients.length > 0
errors.add(:newDomain, "There cannot be any emails already in the database with the target domain" )
end
end
end
但是它似乎没有检查格式,或者甚至没有进入“ cannot_be_present”验证。 (我已经添加了调试语句)
正在使用f.input行中的“必需”位,因为它具有“ *”含义,这是必需的。它只是不抱怨它是否不存在...