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

New error objects in rails 6.1 #93

Open
cherbst-2112 opened this issue Jan 12, 2021 · 10 comments
Open

New error objects in rails 6.1 #93

cherbst-2112 opened this issue Jan 12, 2021 · 10 comments

Comments

@cherbst-2112
Copy link

Complete Description of Issue

Hi, I'm trying to upgrade my reform 2.3 app to rails 6.1 and seeing a few issues. I found #86 but did not see any updates so thought I'd ask here if anyone had any ideas.

Rails 6.1 has a new errors object so they should use the new dsl, there are a few convenience methods to maintain compatibility but they are not working well with reform. Specifically form.errors[:some_field] if I want to see the errors on some field (it returns nil).

Steps to reproduce

I reproduced the problem in a new demo rails 6.1 app, here are the relevant parts:

ActiveRecord::Schema.define(version: 2021_01_12_190713) do

  create_table "things", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

end
class ThingForm < Reform::Form
  property :name
  validates :name, presence: true
end
class ThingsController < ApplicationController
...
  # POST /things
  # POST /things.json
  def create
    @form = ThingForm.new(Thing.new)

    respond_to do |format|
      # HERE, if name is NULL, there is no error indexed with the field name, but there is an error present.
      if @form.validate(thing_params)
        @form.save

        format.html { redirect_to @thing, notice: 'Thing was successfully created.' }
        format.json { render :show, status: :created, location: @thing }
      else
        format.html { render :new }
        format.json { render json: @thing.errors, status: :unprocessable_entity }
      end
    end
  end

Expected behavior

If I add a normal active model validation the model will have an error:

class Thing < ApplicationRecord
  validates :name, { presence: true }
end
(byebug) @form.model.errors[:name]
["can't be blank"]

Actual behavior

The form will not have an error for name:

(byebug) thing_params
#<ActionController::Parameters {"name"=>""} permitted: true>
(byebug) @form.validate(thing_params)
false
(byebug) @form.errors[:name]
[]

Some methods will work correctly so it is a little confusing

(byebug) @form.errors.full_messages
["Name can't be blank"]

System configuration

Reform version:
reform 2.3.3
reform-rails 0.2.1

@jamesbebbington
Copy link

Yes, I can confirm seeing the same issue when attempting to upgrade to rails 6.1. @dotted_errors in Reform::Contract::Result::Errors doesn't seem to be being set, not sure why though.

@marcelolx
Copy link
Contributor

@cherbst-2112 @jamesbebbington As I mentioned here #86 (comment), I think the best approach to fix this is reform-rails to have its own implementation of filter_for, or send down to reform the data as reform expects.

Anyway, this issue is related to reform-rails and not reform, lets keep track of this issue here #86

@skcc321
Copy link

skcc321 commented Feb 12, 2021

Guys you can use dry validation stand-alone. It has everything what you need for this case

@cherbst-2112
Copy link
Author

cherbst-2112 commented Feb 12, 2021

And includes it's own DSL :)

I considered porting all my validations to dry-validations instead but reworking them them to the new DSL was a heavier lift than fixing this library. I've posted a monkey patch in the linked reform-rails issue but there is some contention about wether it belongs there or here. The actual code change was on a class from this repo, so I think here.

@apotonick
Copy link
Member

apotonick commented Nov 21, 2021

Hi there, I'm working on Reform 3.0 and I wonder if there's any place where I can find a complete definition of the errors API in Rails, or maybe even how you folks use it. I know there's

  1. errors[:title] # local form's property
  2. errors.messages # hash of local errors

but I know there's more, like the dotted errors for nested forms, etc. Can anyone point me to better docs or simply paste a few more usage examples? 🍻

@marcelolx
Copy link
Contributor

@apotonick
Copy link
Member

@marcelolx 💯 Thanks! ❤️ Checked it out yesterday. I remember something like those dotted errors in nested setups - do you know anything about that? Also, what if you have a nested form and you do form.errors[:nested_form_name]?

I am making massive progress with Reform 3 and will need testers very very soon (like by the end of the week or so). Anyone? It will be in Trailblazer::Reform namespace so you can use both versions.

@marcelolx
Copy link
Contributor

@apotonick I'm not aware of those dotted errors, as far as I can remember in my prior job for nested forms we used model.errors[:nested_model]

@apotonick apotonick transferred this issue from trailblazer/reform Nov 24, 2021
@cherbst-2112
Copy link
Author

Closing as I'm no longer using reform.

@apotonick apotonick reopened this Feb 9, 2023
@apotonick
Copy link
Member

Thanks for maintaining this ticket, but we're addressing this very problem neatly in Reform 3.

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

5 participants