Ruby-on-rails 仅当存在时才验证属性(仅当用户填写时)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7868067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:13:52  来源:igfitidea点击:

Validate attribute only if it present (only if user fill in it)

ruby-on-railsrubyvalidation

提问by prosto.vint

I need validate some attributes ONLY if they are not empty.

仅当某些属性不为空时,我才需要验证它们。

For example the user may have a logo. If we try to load it - validation should work. If we simply update the user's data without the logo, validation must be skipped.

例如,用户可能有一个标志。如果我们尝试加载它 - 验证应该有效。如果我们只是在没有徽标的情况下更新用户数据,则必须跳过验证。

Now i have:

我现在有:

The form has a choice of two files. One - logo, second - avatar. Both of this attributes is part of User model. In User model a have validation:

该表单可以选择两个文件。一个 - 标志,第二个 - 头像。这两个属性都是用户模型的一部分。在用户模型中有验证:

validates_preference_of :logo_file_name, :message=>I18n.t("...")
validates_format_of :logo_file_name, :with=>/\.(jpeg|jpg|png|gif)$/i, :message=> I18n.t("...")
validates_preference_of :avatar_file_name, :message=>I18n.t("...")
validates_format_of :avatar_file_name, :with=>/\.(jpeg|jpg|png|gif)$/i, :message=> I18n.t("...")

In this case, if we try to create a new User without selected logo and avatar, we will have errors (our validation). I tryed change validation and add ":on => :update" like this:

在这种情况下,如果我们尝试在没有选择徽标和头像的情况下创建新用户,则会出现错误(我们的验证)。我尝试更改验证并添加“:on => :update”,如下所示:

validates_preference_of :logo_file_name, :message=>I18n.t("..."), :on => :update
validates_format_of :logo_file_name, :with=>/\.(jpeg|jpg|png|gif)$/i, :message=> I18n.t("..."), :on => :update
validates_preference_of :avatar_file_name, :message=>I18n.t("..."), :on => :update
validates_format_of :avatar_file_name, :with=>/\.(jpeg|jpg|png|gif)$/i, :message=> I18n.t("..."), :on => :update

Now i can create user without selected logo and avatar, but if i try edit user and try upload only logo - i have validation errors of avatar. If i choose file for avatar and logo leave blank - i have validation errors for logo.

现在我可以创建没有选定徽标和头像的用户,但是如果我尝试编辑用户并尝试仅上传徽标 - 我的头像验证错误。如果我为头像和徽标选择文件留空 - 我有徽标验证错误。

How i can run validation ony for attribute that I want to change?

如何对要更改的属性运行验证?

回答by Mark Thomas

Add :allow_blank => trueand it should do what you want.

添加:allow_blank => true它应该做你想做的。

回答by S?awosz

Maybe :if => lambda {|attr| attr.present?}will help?

也许:if => lambda {|attr| attr.present?}会有所帮助?

回答by miguel.camba

Some validations accept the options :allow_blank => trueor :allow_nil => true.

一些验证接受选项:allow_blank => true:allow_nil => true

If this fails, use :if condition, like this:

如果失败,请使用 :if 条件,如下所示:

validates_format_of :avatar_file_name, 
 :with=>/\.(jpeg|jpg|png|gif)$/i, 
 :message=> I18n.t("..."), 
 :on => :update,
 :if => lambda{ |object| object.avatar_file_name.present? }

But i encourage you to use allows. Much cleaner.

但我鼓励你使用允许。干净多了。