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

Check the form for any errors #87

Open
TarasBardiuk opened this issue Jan 11, 2021 · 6 comments
Open

Check the form for any errors #87

TarasBardiuk opened this issue Jan 11, 2021 · 6 comments

Comments

@TarasBardiuk
Copy link

TarasBardiuk commented Jan 11, 2021

I'm working on migrating from reform 2.2.4 to 2.3.3 and from reform-rails 0.1.7 to 0.2.1.

In some places we have a code like this:

form = UserForm.new(user)
form.tap do |form|
  form.validate(params)
  form.errors.add(:base, :custom_error)
end

#####
> form.errors.present?
=> false
> form.errors.any?
=> true

The problem is, that if error was added AFTER form.validate(params) invocation, I cannot check errors existence using form.errors.present?. I guess it is because

# activesupport-6.0.2.1/lib/active_support/core_ext/object/blank.rb
def present?
  !blank?
end

# activesupport-6.0.2.1/lib/active_support/core_ext/object/blank.rb
def blank?
  respond_to?(:empty?) ? !!empty? : !self
end

# reform-rails-0.2.1/lib/reform/form/active_model/validations.rb
def empty?
  @success
end

and @success is not changed if error is added after validation. Therefore, I cannot use form.errors.empty? as well.

Alternatively we can use any? method. But!

It doesn't works as expected in other place where I use nested form:

class ProductForm
  property :data, form: ProductDataForm
end

#####
> form = ProductForm.new(product)
> form.validate(data: { attribute: 'invalid' })
=> false
> form.errors.present?
=> true
> form.errors.any?
=> false

So how should I check if there is any error including nested forms errors or errors added manually after validation?

Thanks in advance.

@TarasBardiuk
Copy link
Author

@apotonick, @emaglio, @seuros, could you take a look?

@marcelolx
Copy link
Contributor

marcelolx commented Jan 12, 2021

@TarasBardiuk I think as a workaround you can use form.errors.full_messages.any? to check if there is any error, this will work in both cases.

And to fix this issue I think that we need set @success to false here https://github.com/trailblazer/reform-rails/blob/master/lib/reform/form/active_model/validations.rb#L144 when adding an error message 🤔 But I'm not sure if this won't cause some unexpected side effects

@TarasBardiuk
Copy link
Author

@TarasBardiuk I think as a workaround you can use form.errors.full_messages.any? to check if there is any error, this will work in both cases.

And to fix this issue I think that we need set @success to false here https://github.com/trailblazer/reform-rails/blob/master/lib/reform/form/active_model/validations.rb#L144 when adding an error message 🤔 But I'm not sure if this won't cause some unexpected side effects

@marcelolx thanks for reply. Your suggestion to change the add method helped, as well as my version with changing the empty? method from @success to @success && messages.empty?. But none of them fixes form.errors.any? => false problem when validation fails in nested form.

@marcelolx
Copy link
Contributor

@TarasBardiuk Even form.errors.full_messages.any? didn't worked when nested form validation fails? As I mentioned in my last reply, I think you could use form.errors.full_messages.any? instead of form.errors.any? as a workaround while we think in a way to fix empty? behavior.

@TarasBardiuk
Copy link
Author

@TarasBardiuk Even form.errors.full_messages.any? didn't worked when nested form validation fails?

No-no, it works, but it's not very convenient) But before the update, form.errors.any? worked successfully in any cases, and now it is a little broken.

@akichim21
Copy link

Hi.
Are you planning to modify this use case?
I'm using it in controller and would like to hear how you plan to handle it.

form = UserForm.new(user)
form.tap do |form|
  form.validate(params)
  form.errors.add(:base, :custom_error)
end

form.errors.present? -> expect true
form.errors.empty? -> expect false
if form.errors.empty?
  render success
else
  render error
end

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