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

#empty? does not update if error added via #errors.add(...) #104

Open
richardboehme opened this issue Aug 18, 2023 · 1 comment
Open

#empty? does not update if error added via #errors.add(...) #104

richardboehme opened this issue Aug 18, 2023 · 1 comment

Comments

@richardboehme
Copy link
Collaborator

Using #empty? is a common pattern to check if a form object has any errors or not. For example this method is also used when calling #present?. It should return true if there are no errors and false otherwise.

Currently I'm working on an application that requires adding errors to the form after it was validated. However, adding an error after validating the form does not change the status of #empty?. The following example demonstrates the problem:

class Form < Reform::Form
  property :example
end

form = Form.new(OpenStruct.new)
form.validate({example: 1})
form.errors.empty? # => true
form.errors.add(:example, :invalid)
form.errors.empty? # => true

The last call to #empty? should return false. The same issue also exists when adding a new error to a nested form.

Currently #empty? only checks for the @success instance variable. My first idea was to set this instance variable to false once #add is called on the form's errors. However, this won't work for nested forms because the "parent"-form still does not know about the nested error. Maybe there is another better way of solving this issue?

The current workaround for me is to call validate({}) after adding the errors to ensure the new errors are "recognized" by the form.

@richardboehme
Copy link
Collaborator Author

Ok calling revalidating the form as a workaround has it's own problems. Just want to share my current workaround patch that seems to work:

module ReformErrorsPatch
  def empty?
    @form.schema.each(twin: true) do |dfn|
      Disposable::Twin::PropertyProcessor.new(dfn, @form).() do |_frm, i|
        form_obj = i ? @form.send(dfn[:name])[i] : @form.send(dfn[:name])
        return false unless form_obj.errors.empty?
      end
    end
    messages.empty?
  end
end

Rails.application.config.to_prepare do
  Reform::Form::ActiveModel::Validations::Result::ResultErrors.prepend(ReformErrorsPatch)
end

This definitely comes with some performance penalty. I guess there is a simpler way to do this.

@richardboehme richardboehme changed the title #empty? does not update if error added via `#errors.add(...) #empty? does not update if error added via #errors.add(...) Aug 18, 2023
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

1 participant