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

Allow custom error on forms without model #1585

Merged
merged 1 commit into from
Jun 9, 2018

Conversation

victorperez
Copy link
Contributor

@victorperez victorperez commented May 24, 2018

This PR change the has_errors? method to allow custom errors on forms without a model:

Before this PR

= simple_form_for :user, url: users_path do |f|
  = f.input :email, error: 'custom error'
Output
...
<div class="form-group email optional user_email">
  <label class="control-label email optional" for="user_email">Email</label>
  <input class="form-control string email optional" type="email" name="user[email]" id="user_email">
</div>
...

After this PR

= simple_form_for :user, url: users_path do |f|
  = f.input :email, error: 'custom error'
Output
<div class="form-group email optional user_email has-error">
  <label class="control-label email optional" for="user_email">Email</label>
  <input class="form-control string email optional" aria-invalid="true" type="email" name="user[email]" id="user_email">
  <span class="help-block">custom error</span>
</div>

@feliperenan
Copy link
Collaborator

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?

@victorperez
Copy link
Contributor Author

victorperez commented May 28, 2018

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 simple_form without a model and when the form is submitted it will send a request through an API to the second application where the model/validations exists, the second application will create the record or return the errors in case the form is invalid.

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:

= simple_form_for :user, url: users_path do |f|
  ...
  = f.input :first_name, error: @errors[:first_name]
  ...

If @errors[:first_name] is nil no error will be displayed and if @errors[:first_name] has a value from the api response then the error will be displayed.

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 :)

@feliperenan
Copy link
Collaborator

Thanks for the explanation, now it makes sense to me 👍.

@@ -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?
Copy link
Collaborator

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?

Copy link
Contributor Author

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)

Copy link
Contributor Author

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.

@@ -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?
Copy link
Collaborator

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?

Copy link
Contributor Author

@victorperez victorperez May 28, 2018

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

Copy link
Collaborator

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?

Copy link
Contributor Author

@victorperez victorperez May 28, 2018

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
@feliperenan feliperenan merged commit 501229c into heartcombo:master Jun 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants