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

Regression in 2.3 when adding errors in an overridden setter method #509

Open
shepmaster opened this issue May 26, 2020 · 0 comments
Open
Assignees

Comments

@shepmaster
Copy link
Contributor

Complete Description of Issue

This code no longer produces validation failures after upgrading to 2.3

Steps to reproduce

Gemfile

# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

# Works with this version
gem "reform", '2.2.4'
gem "reform-rails", '~> 0.1', require: false

# Fails with this version
#gem "reform", '2.3.1'
#gem "reform-rails", '~> 0.2', require: false

gem 'dry-types', '~> 0.14.1'

repro.rb

# frozen_string_literal: true

Bundler.require

### BEGIN COPY-PASTE FROM REFORM-RAILS
require "reform"
require "reform/form/active_model/model_validations"
require "reform/form/active_model/form_builder_methods"
require "reform/form/active_model"
require "reform/form/active_model/validations"
require "reform/form/multi_parameter_attributes"

require "reform/active_record" if defined?(ActiveRecord)
require "reform/mongoid" if defined?(Mongoid)

Reform::Form.class_eval do
  include Reform::Form::ActiveModel
  include Reform::Form::ActiveModel::FormBuilderMethods
  include Reform::Form::ActiveRecord if defined?(ActiveRecord)
  include Reform::Form::Mongoid if defined?(Mongoid)
  include Reform::Form::ActiveModel::Validations
end
### END COPY-PASTE

Example = Struct.new(:sorts)

class ExampleForm < Reform::Form
  VALID_SORTS = %w[name date].freeze
  VALID_SORT_DIRECTIONS = %w[asc desc].freeze

  property :sorts, default: [{ by: :name, dir: :desc }]

  def sorts=(sort_strings)
    sorts = sort_strings.split(',').map do |sort|
      by, dir = sort.split(':')

      errors[:sort] << "is not valid" unless VALID_SORTS.include?(by)
      errors[:sort_direction] << "is not valid" unless VALID_SORT_DIRECTIONS.include?(dir)

      { by: by.underscore.to_sym, dir: dir.to_sym }
    end

    super(sorts)
  end
end

model = Example.new
form = ExampleForm.new(model)

res = form.validate({ sorts: 'wutang:asc' })

raise "Should be false" if res
% bundle
% bundle exec ruby repro.rb

# Edit the gemfile to switch to reform 2.3

% bundle update
% bundle exec ruby repro.rb

Expected behavior

The input parameters should be invalid.

Actual behavior

The errors added are lost somewhere and the form is marked as valid.

System configuration

Reform version: Upgrading from 2.2.4 to 2.3.1
Reform-Rails version: Upgrading from 0.1 to 0.2

@seuros seuros self-assigned this May 26, 2020
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

2 participants