Rails 验证上下文

发布于 2024-11-07 23:19:53 字数 1461 浏览 1 评论 0原文

我的 ActiveRecord 模型需要帮助。我有基于上下文的验证(错误),使用内置上下文选项进行验证:

validates :foo, :on => :bar, :presence => true

model = Model.new
model.foo = nil
model.valid? # => true
model.save # works as expected

model.valid?(:bar) # => false
model.save(:context => :bar) # fails and returns false

但是在 accepts_nested_attributes_for :model 中使用我的模型并调用 parent.save 失败(验证被调用并返回 false),有什么建议或解决方案吗?


仍然没有答案?为了更多地解释我的问题:我有一个名为 Form 的模型,它有许多 Field 。用户应该在提交时看到验证错误,但无论如何都应该保存表单(无论有没有错误)。有不同类型的Field,每种类型都有全局验证(以确保数据库一致性)和其自己特定的用户定义验证(以验证用户输入的数据)。所以我的 Field 看起来像这样:

 # Global validations, to ensure database consistency
 # If this validations fail, the record should not be saved!
 validates_associated :form, :on => :global
 ...

 # Specific user-defined validations
 # If this validations fail, the record should be saved but marked as invalid. (Which is done by a before_save filter btw.)
 def validate
   validations.each do |validation| # Array of `ActiveModel::Validations`, defined by the user and stored in a hash in the database
     validation.new(:on => :specific).validate(self)
   end
 end

在我的控制器中:

 # def create
 # ...
 form.attributes = params[:form]
 form.save!(:global)
 form.save(:specific)

使用内置功能在 Rails 中是否可以实现类似的功能?顺便说一句,这不是我的实际代码,它非常复杂。但我希望你们能明白这个想法。

I need help with my ActiveRecord model. I have context based validations (mis)using the build-in context options for validations:

validates :foo, :on => :bar, :presence => true

model = Model.new
model.foo = nil
model.valid? # => true
model.save # works as expected

model.valid?(:bar) # => false
model.save(:context => :bar) # fails and returns false

But using my model in a accepts_nested_attributes_for :model and calling parent.save fails (the validation gets called and returns false), any suggestions or solutions?


Still no answer? To explain more about my problem: I have a model called Form which has many Fields. Users should see validation errors on submit, but the form should be saved anyway (with and without errors). There are different types of Fields, each with global validations (to ensure database consistency) and its own specific user-defined validations (to validate user-entered data). So my Fields look someway like that:

 # Global validations, to ensure database consistency
 # If this validations fail, the record should not be saved!
 validates_associated :form, :on => :global
 ...

 # Specific user-defined validations
 # If this validations fail, the record should be saved but marked as invalid. (Which is done by a before_save filter btw.)
 def validate
   validations.each do |validation| # Array of `ActiveModel::Validations`, defined by the user and stored in a hash in the database
     validation.new(:on => :specific).validate(self)
   end
 end

In my controller:

 # def create
 # ...
 form.attributes = params[:form]
 form.save!(:global)
 form.save(:specific)

Is something similar possible in Rails using the built-in functionality? Btw this not my actual code, which is quite complicated. But I hope, you guys will get the idea.

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

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

发布评论

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

评论(4

浪菊怪哟 2024-11-14 23:19:54

仅适用于 Rails 5+:

您正在寻找

with_options on: :custom_event do
  validates :foo, presence: true
  validates :baz, inclusion: { in: ['b', 'c'] }
end

验证或保存使用

model = YourModel.new

# Either
model.valid?(:custom_event)

# Or
model.save(context: :custom_event)

Only for Rails 5+:

You are looking for

with_options on: :custom_event do
  validates :foo, presence: true
  validates :baz, inclusion: { in: ['b', 'c'] }
end

To validate or save use

model = YourModel.new

# Either
model.valid?(:custom_event)

# Or
model.save(context: :custom_event)
昵称有卵用 2024-11-14 23:19:54

has_nested_attributes_for :model 更改为 accepts_nested_attributes_for :models

希望这有帮助。

祝你好运。

Change has_nested_attributes_for :model to accepts_nested_attributes_for :models.

Hope this helps.

Good luck.

小耗子 2024-11-14 23:19:53

尝试条件验证

class Customer 
  attr_accessor :managing 

  validates_presence_of :first_name
  validates_presence_of :last_name 

  with_options :unless => :managing do |o|
    o.validates_inclusion_of :city, :in=> ["San Diego","Rochester"]
    o.validates_length_of :biography, :minimum => 100 
  end
end

@customer.managing = true
@customer.attributes = params[:customer]
@customer.save

Try conditional validations

class Customer 
  attr_accessor :managing 

  validates_presence_of :first_name
  validates_presence_of :last_name 

  with_options :unless => :managing do |o|
    o.validates_inclusion_of :city, :in=> ["San Diego","Rochester"]
    o.validates_length_of :biography, :minimum => 100 
  end
end

@customer.managing = true
@customer.attributes = params[:customer]
@customer.save
哆兒滾 2024-11-14 23:19:53

Rails 4.1 中引入了“定义验证时指定多个上下文的能力” - 检查 验证方法,:on 选项描述

"Ability to specify multiple contexts when defining a validation" was introduced in Rails 4.1 - check validate method, :on options description

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