我的模型abc.rb有两个验证
validates_format_of :url, :with => URI::regexp(%w(http https))
validates_format_of :targetUrl, :with => URI::regexp(%w(http https))
validates_format_of :targetUrl
不验证输入。 无论我输入什么,它都可以接受。我在进行验证的其他模型上遇到的相同问题 validates_format_of :website, :with => URI::regexp(%w(http https))
. 我无法弄清楚为什么验证失败
validates_format_of :targetUrl, :with => URI::regexp(%w(http https))
和validates_format_of :website, :with => URI::regexp(%w(http https))
.
任何提示都会非常有帮助。谢谢
您不需要以这种方式指定正则表达式格式。使用类似以下内容:
validates :website, format: { with: /(http|https)://[a-zA-Z0-9-#/_]+[.][a-zA-Z0-9-.#/_]+/i }
显然使用您喜欢的任何正则表达式!