reform-rails
reform-rails copied to clipboard
validates: { ... } duplicate error messages
In my form I have such property and its validations declaration:
properties :group, :manager,
validates: { presence: true, length: 1..12 }
property :warehouse,
validates: { presence: true, length: 2..8 }
Both when declaring property with property
or properties
validation errors are duplicate. However, when I separate validations from property declaration:
property :warehouse
validates :warehouse, presence: true, length: 2..8
Duplication are no longer there. So it seems that validates: { ... }
causes error duplication.
I know this issue was reported in trailblazer/trailblazer #109 and was closed as it seemed that updating from reform 2.1 to 2.2 solved it. In my case i use reform-rails 0.1.7 and reform 2.2 and problem is still there or was reintroduced.
I have the same issue using reform 2.3.0rc1 and reform-rails 0.2.0rc2 on Rails 5.2.1.
The separate validates
also works just fine.
I just tested it with the same reform and reform-rails version for a Rails 4.2.10 app and there the problem does not occur.
I know it's an old issue but it seems like I'm still seeing this error when inheriting a property from a contract.
The following example leads to the problem:
class Parent < Reform::Form
property :title, validates: { presence: true }
end
class Child < Parent
end
contract = Child.new(OpenStruct.new)
contract.validate({})
contract.errors.details
# => { title: [{ error: :blank }, { error: :blank }] }
I looked into it and seems to be related to the heritage system of disposable. We inherit property definitions AND validations from the parent form. This leads to duplicate validation creation in the child contract because it will receive the validation from the parent property definition and the parent validation definition.
I tried fixing this by deleting the validates
option from the property in https://github.com/trailblazer/reform/blob/master/lib/reform/contract.rb#L22 (using options.delete(:validates)
instead of options[:validates]
). But I'm not sure if this is an appropriate fix. This way it would not copy the validation from the inherited property.
Could you help me/us with this? @apotonick