在 Rails 中创建期间跳过一些验证,但运行其他验证

发布于 2025-01-08 17:57:29 字数 570 浏览 0 评论 0原文

我想在创建新用户期间跳过一些属性的验证,例如地址、密码、电话号码等。
但是,当用户尝试编辑模型时,它仍然需要在模型中进行其他验证。我尝试使用 :on => :更新但这对我没有帮助。有什么建议吗?

我的代码:

validates :address, :presence => true, :length => { :maximum => 50 }, :on => :update 
validates :city, :presence => true, :length => { :maximum => 50 }, :on => :update 
validates :state, :presence => true, :length => { :maximum => 50 }, :on => :update 
validates :zip, :presence => true, :numericality => true, :on => :update, :length => { :is => 5 }

I want to skip the validations of few attributes during creation of a new user like address, pin, phone number etc.
However, it still need to do the other validations in the model when user tries to edit it. I tried using :on => :update but that doesn't help me. Any suggestions ?

My Code:

validates :address, :presence => true, :length => { :maximum => 50 }, :on => :update 
validates :city, :presence => true, :length => { :maximum => 50 }, :on => :update 
validates :state, :presence => true, :length => { :maximum => 50 }, :on => :update 
validates :zip, :presence => true, :numericality => true, :on => :update, :length => { :is => 5 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

咆哮 2025-01-15 17:57:29

根据文档,您需要做的是这样的。你是说这行不通吗?

class Person < ActiveRecord::Base
  validates_presence_of :address, :on => :update
  validates_presence_of :pin,     :on => :update
end

According the the documentation, what you need to do is something like this. Are you saying this is not working?

class Person < ActiveRecord::Base
  validates_presence_of :address, :on => :update
  validates_presence_of :pin,     :on => :update
end
欢烬 2025-01-15 17:57:29

可以通过传递 :validate => 来跳过保存时的验证过程。
错误。

请注意,如果存在数据库限制,您仍然会收到错误消息。
例如,如果您使用 Rails 迁移并具有 :null => false 创建时(通过运行迁移),实际数据库列将在数据库级别具有该限制。一件好事是验证应该在两个地方进行。覆盖数据库约束(即不能)的方法是迁移以实际删除约束。

The validation process on save can be skipped by passing :validate =>
false.

Note that is there are database constraints you'll still get an error.
e.g. if you use a rails migration and have :null => false when it is created (by running the migration) the actual database column will have that restriction be at the database level. A good thing as validations should be in both places. The way to override the db constrainst (i.e. you can't) would be a migration to actually remove the constraint.

青春有你 2025-01-15 17:57:29

创建记录时:

@model = Model.new(params[:model])
@model.save false

这将跳过验证。

On creation of record:

@model = Model.new(params[:model])
@model.save false

This will skip the validation.

缘字诀 2025-01-15 17:57:29
validates :address, :presence => true,
                      :length => { :maximum => 50 },
                      :if => :address_changed?

  validates :city, :presence => true,
                   :length => { :maximum => 50 },
                   :if => :city_changed?

  validates :state, :presence => true,
                    :length => { :maximum => 50 },
                    :if => :state_changed?

  validates :zip,   :presence => true,
                    :numericality => true,
                    :length => { :is => 5 },
                    :if => :zip_changed?

添加 if=> :属性改变了?将解决问题。

validates :address, :presence => true,
                      :length => { :maximum => 50 },
                      :if => :address_changed?

  validates :city, :presence => true,
                   :length => { :maximum => 50 },
                   :if => :city_changed?

  validates :state, :presence => true,
                    :length => { :maximum => 50 },
                    :if => :state_changed?

  validates :zip,   :presence => true,
                    :numericality => true,
                    :length => { :is => 5 },
                    :if => :zip_changed?

Adding if=> :attribute_changed? will solve the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文