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

Add :valid_class on input wrapper when value is present and valid #1333

Closed

Conversation

aeberlin
Copy link

Purpose

The purpose of this PR is to improve style support for inputs with validations. Simply put, the lack of an applied :error_class is not indicative of the attribute being valid. While this isn't a guaranteed method of determining if it is valid, it is as close as programmatically possible (AFAIK). See caveats below.

Logic

When a :valid_class is specified, it will be applied to an input wrapper given the following conditions:

  1. Object exists.
  2. Object responds to :errors.
  3. There are no validation errors (e.g. validations passed or were not specified).
  4. Object responds to the attribute method.
  5. Return value of attribute method is present.
Changes
  1. Added :valid_class declarations (similar to those for :error_class) on default wrappers.
  2. Added :has_value? and :valid? to SimpleForm::Components::Errors.
  3. Adjusted SimpleForm::Wrappers::Root#html_classes to apply :valid_class when applicable.
Known caveats

If an attribute is valid when :blank?, the :valid_class will not be applied. I am open to suggestions here, as this is the only hole in my logic, albeit an acceptable and predictable one (IMO).

Further notes

I adjusted two failing tests that were missing stubs, and added another test for this feature.

Please let me know if there's something else I should include. Thanks, cheers!

@aeberlin
Copy link
Author

@plataformatec Feedback, please?

Copy link
Collaborator

@feliperenan feliperenan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @aeberlin

Looks good to me. I tested it and it works as expected. The cool thing is that if you don't want to use this feature you can remove the valid_class from the initializer.

Thanks for the pull-request :)

@rafaelfranca Any thougths?

@@ -243,4 +243,16 @@ def with_full_error_for(object, *args)
assert_no_select 'span.error'
end
end

# NO ERRORS
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can remove this comentary

@@ -13,6 +13,16 @@ def has_errors?
object && object.respond_to?(:errors) && errors.present?
end

def has_value?
return unless object && object.respond_to?(attribute_name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we expect a boolean here we can return false. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole method could be a a single operation:

object && object.respond_to?(attribute_name) && object.send(attribute_name).present?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josevalim - Done ✅ 12f38ee

@m5o
Copy link
Contributor

m5o commented Mar 6, 2018

@feliperenan would you may take over this PR? It seems @aeberlin is not very active, except probably no response. Did he allow PR edits from maintainers? Otherwise, I would volunteer, with your guidance.

@rafaelfranca
Copy link
Collaborator

👍 I'm good with the idea

@feliperenan
Copy link
Collaborator

feliperenan commented Mar 6, 2018

@m5o We can't commit on this branch as well. If you are willing in working on it go ahead, just open another pull request then we can close this one.

m5o added a commit to m5o/simple_form that referenced this pull request Mar 13, 2018
* use single operation for has_value?
  * via: comment from @josevalim at: heartcombo#1333 (comment)
@feliperenan
Copy link
Collaborator

closing in favor #1553

m5o added a commit to m5o/simple_form that referenced this pull request Mar 23, 2018
* use single operation for has_value?
  * via: comment from @josevalim at: heartcombo#1333 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants