-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Allow custom error on forms without model #1585
Conversation
e3fa03c
to
b1be461
Compare
Hi @victorperez, thanks for the pull request :) Using the custom error option on a non-model the form ever will be displayed with errors since we don't have validations inside this kind of object, so I'm not sure if it makes sense with a non-model. Could you explain what use case motivated you to send this PR? |
Hi @feliperenan, thanks for the feedback. Consider the case where you have two applications in your stack, the main application has a view that use If errors are returned from the second app then will be easy to create the errors in the form if we allow custom errors for forms without a model. Of course the idea is to pass a dynamic variable based in the API response to the input (not a hardcoded string that always display the error in the form) ej:
If It can be fixed creating a model in the main application to handle the form or using activeresource (https://github.com/rails/activeresource) which is what I finally did in my project, but I consider to open a PR because maybe it could be useful. I understand that this is not a common scenario or maybe there is a better approach to handle what I'm exposing so feel free to take the best decision :) |
Thanks for the explanation, now it makes sense to me 👍. |
lib/simple_form/components/errors.rb
Outdated
@@ -34,6 +34,14 @@ def full_error_text | |||
has_custom_error? ? options[:error] : full_errors.send(error_method) | |||
end | |||
|
|||
def form_with_object_has_errors? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could name this method of model_with_errors?
, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I like more that name, but what you think to call it object_with_errors?
(just to respect the same variable name that we use in the method)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, I have amended my commit and renamed the method.
lib/simple_form/components/errors.rb
Outdated
@@ -11,7 +11,7 @@ def full_error(wrapper_options = nil) | |||
end | |||
|
|||
def has_errors? | |||
object && object.respond_to?(:errors) && errors.present? | |||
form_with_object_has_errors? || form_without_object_has_errors? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to check if the object is nil at the second condition?, just check has_custom_errors?
would be enough, is it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to check if the object is nil
to respect other logic based in forms using an object and if there is no error on the attribute. If the object.nil?
is removed from the second condition then we have two test failing.
Test 1: https://github.com/victorperez/simple_form/blob/master/test/form_builder/error_test.rb#L221
Test 2: https://github.com/victorperez/simple_form/blob/master/test/form_builder/error_test.rb#L274
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we could rename this method or just use the conditions here, something like this:
object_with_errors? || object.nil? && has_custom_error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, I have amended my commit and changed the conditions to what you propose 👍
This PR change the has_errors? method to allow custom errors on forms without a model
This PR change the
has_errors?
method to allow custom errors on forms without a model:Before this PR
Output
After this PR
Output