Created
May 13, 2015 11:13
-
-
Save scarfacedeb/cda3cda4db1f16d45e1f to your computer and use it in GitHub Desktop.
Helper method to check validation errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# required: Rails 5 or active_model-errors_details gem | |
# see: https://cowbell-labs.com/2015-01-22-active-model-errors-details.html | |
module CheckValidationDetails | |
# Check if the given error exists on given attribute | |
# | |
# @param attribute [Symbol] model attribute | |
# @param error [Symbol] machine-friendly error name | |
# @return [Boolean] whether or not any given error exists | |
def error?(attribute, error) | |
errors.details.key?(attribute) && errors.details[attribute].any? { |err| err[:error] == error } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment