如何验证数组字段的成员?
我有这个模型:
class Campaign
include Mongoid::Document
include Mongoid::Timestamps
field :name, :type => String
field :subdomain, :type => String
field :intro, :type => String
field :body, :type => String
field :emails, :type => Array
end
现在我想验证 emails
数组中的每封电子邮件的格式是否正确。我阅读了 Mongoid 和 ActiveModel::Validations 文档,但没有找到如何执行此操作。
你能给我指点一下吗?
I have this model:
class Campaign
include Mongoid::Document
include Mongoid::Timestamps
field :name, :type => String
field :subdomain, :type => String
field :intro, :type => String
field :body, :type => String
field :emails, :type => Array
end
Now I want to validate that each email in the emails
array is formatted correctly. I read the Mongoid and ActiveModel::Validations documentation but I didn't find how to do this.
Can you show me a pointer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以定义自定义
ArrayValidator
。将以下内容放入app/validators/array_validator.rb
中:您可以在模型中像这样使用它:
它将根据 每个 验证数组中的每个元素在
array
哈希中指定的strong> 验证器。You can define custom
ArrayValidator
. Place following inapp/validators/array_validator.rb
:You can use it like this in your models:
It will validate each element from the array against every validator specified within
array
hash.Milovan 的答案得到了我的支持,但实现存在一些问题:
展平嵌套数组会更改行为并隐藏无效值。
nil
字段值被视为[nil]
,这似乎不正确。所提供的示例,使用
presence: true
将生成NotImplementedError
错误,因为PresenceValidator
未实现validate_each
.在每次验证时为数组中的每个值实例化一个新的验证器实例效率相当低。
生成的错误消息不会显示数组元素无效的原因,这会造成较差的用户体验。
生成
这是一个更新的可枚举和数组验证器,可以解决所有这些问题。为了方便起见,代码包含在下面。
Milovan's answer got an upvote from me but the implementation has a few problems:
Flattening nested arrays changes behavior and hides invalid values.
nil
field values are treated as[nil]
, which doesn't seem right.The provided example, with
presence: true
will generate aNotImplementedError
error becausePresenceValidator
does not implementvalidate_each
.Instantiating a new validator instance for every value in the array on every validation is rather inefficient.
The generated error messages do not show why element of the array is invalid, which creates a poor user experience.
Here is an updated enumerable and array validator that addresses all these issues. The code is included below for convenience.
您可能想为电子邮件字段定义自己的自定义验证器。
因此,您将在类定义之后添加,
正则表达式本身可能并不完美,但这是基本思想。您可以按如下方式查看 Rails 指南:
http:// /guides.rubyonrails.org/v2.3.8/activerecord_validations_callbacks.html#creating-custom-validation-methods
You'll probably want to define your own custom validator for the emails field.
So you'll add after your class definition,
The regex itself may not be perfect, but this is the basic idea. You can check out the rails guide as follows:
http://guides.rubyonrails.org/v2.3.8/activerecord_validations_callbacks.html#creating-custom-validation-methods
发现自己刚才正在尝试解决这个问题。我稍微修改了 Tim O 的答案,得出以下内容,它为错误对象提供了更清晰的输出和更多信息,然后您可以在视图中向用户显示这些信息。
Found myself trying to solve this problem just now. I've modified Tim O's answer slightly to come up with the following, which provides cleaner output and more information to the errors object that you can then display to the user in the view.
下面是一个可能对 Rails api 文档有所帮助的示例: http://apidock.com/ Rails/ActiveModel/Validations/ClassMethods/validates
Here's an example that might help out of the rails api docs: http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates