Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validates: { ... } duplicate error messages #59

Open
AurimasLazdauskas opened this issue Feb 22, 2018 · 3 comments
Open

validates: { ... } duplicate error messages #59

AurimasLazdauskas opened this issue Feb 22, 2018 · 3 comments

Comments

@AurimasLazdauskas
Copy link

AurimasLazdauskas commented Feb 22, 2018

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.

@sliminas
Copy link
Contributor

sliminas commented Sep 3, 2018

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.

@sliminas
Copy link
Contributor

sliminas commented Sep 3, 2018

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.

@richardboehme
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants