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

The custom error: on all inputs does not work if there is an object present #1832

Open
matt-whiteley opened this issue Feb 29, 2024 · 0 comments

Comments

@matt-whiteley
Copy link

f.input :title, error: "error" should set a custom error message, but the error is completely ignored unless f.object is nil, which is very unlikely for the vast majority of model backed forms.

The bug was introduced in this commit over 6 years ago 09c928f which was added to support errors working when there is no f.object

https://github.com/heartcombo/simple_form/blob/main/lib/simple_form/components/errors.rb#L13

# lib/simple_form/components/errors.rb#L13
def has_errors?
    object_with_errors? || object.nil? && has_custom_error?
end

The issue is the logic then never reaches has_custom_error? if f.object is present, because it either only looks in the object's errors, or fails the object.nil? check when you have an object.

I propose the fix is to just change this line to

object_with_errors? || has_custom_error?

as the custom error is used if it exists and the object.nil? check was not adding any actual benefit. This will find an error on the object if an object exists, or will fallback to the custom error regardless of object being present or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant