reform-rails: Unable to use `id` in ActiveModel validations since reform-rails 0.2.4

Hi!

We are struggling to update reform-rails from 0.2.3 to 0.2.5 (we also had this same struggle with reform-rails version 0.2.4).

The problem seems to stem from us having this kind of validation:

property :id, virtual: true
property :_destroy, virtual: true
property :photo

validates :_destroy, presence: true, if: -> { id }
validates :id, presence: true, if: -> { _destroy }

And we have this kind of validation call:

form.validate(photo: a_new_image)

With reform-rails 0.2.4 and 0.2.5, we get a validation error saying that _destroy can’t be blank while with 0.2.3 we don’t get that.

The change is that in reform-rails 0.2.3, the value of id in the if: -> { id } lambda was read from the parameters passed to form.validate while in 0.2.5, the value of id is read from the form.model. I think the problem is somehow caused by https://github.com/trailblazer/reform-rails/commit/294de5fbbc32576b47d62ddc852bca356214124d and https://github.com/trailblazer/reform-rails/pull/100 but I don’t really understand why.

Is it possible that with the old Uber::Delegates, the id delegation was somehow different than how it is now with Rails provided delegate method?


Here’s the full source code for our form

# frozen_string_literal: true
module Profiles::Common
  class ProfileEditImagesForm < Reform::Form
    model :profile

    ImagePopulator =
      lambda do |fragment:, collection:, **|
        # `fragment` represents the incoming params piece regarding one image.
        # `collection` represents the methods usable to manage the `images` collection.
        # http://trailblazer.to/gems/reform/populator.html

        if (fragment[:_destroy] && !fragment[:id]) ||
             (fragment[:id] && !fragment[:_destroy])
          # Validations will prevent this kind of populator from going through. We just
          # need this populator to return some valid value so that Reform won't choke yet,
          # but one that allows Reform to provide validation errors anyway. Whew.
          return collection.append(::Profiles::Common::ProfileImage.new)
        end

        if fragment[:_destroy]
          item =
            collection.find { |itm| itm.model.id.to_s == fragment[:id].to_s }
          collection.delete(item) if item
          skip!
        else
          collection.append(::Profiles::Common::ProfileImage.new)
        end
      end

    # `save: false` stops existing images being touched when collection is changed
    collection :images, populator: ImagePopulator, save: false do
      property :id, virtual: true
      property :_destroy, virtual: true
      property :photo

      validates :id, presence: true, if: -> { _destroy }
      validates :_destroy, presence: true, if: -> { id }
    end
  end
end

And here’s how we have a unit test for it:

it 'does not modify existing images' do
  existing_image = create(:image, :with_real_file)
  profile = create(Profiles::SupplierProfile.factory_name, images: [existing_image])
  form = Profiles::Common::ProfileEditImagesForm.new(profile)

  new_image = fixture_file_upload('lol.png', 'image/png')
  result = form.validate(images: [{ photo: new_image }])
  puts "We have errors but shouldn't: #{form.errors.messages}"
  assert(result)
  expect { form.save }.not_to(change { existing_image.reload.updated_at })
end

Back in reform-rails 0.2.3, this code didn’t have errors but now with 0.2.5, we get an error:

We have errors but shouldn't: {:"images._destroy"=>["can't be blank"]}

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 23 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks again for your debugging and reporting, I wish we had more people like you! 💪