From d76d166357c8eb1f7ae1e38c60d603c1b39b5caa Mon Sep 17 00:00:00 2001 From: Moncef Belyamani Date: Thu, 4 Sep 2014 01:24:27 -0400 Subject: [PATCH] Highlight fields with errors & update validations. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #188. Rails automatically surrounds a field that failed to pass validations with a div that has a class called `field_with_errors`. For some reason, this doesn’t get added to serialized fields, so I had to write custom methods in `form_helper.rb`. I also updated the HTML for some of the fields so that the highlighted field would retain the same appearance as the regular field. While working on this, I realized some of the model validations could be refactored and simplified. --- app/assets/stylesheets/application.css.scss | 10 ++++++++++ app/helpers/admin/form_helper.rb | 9 +++++++++ app/models/location.rb | 12 +++--------- app/models/organization.rb | 5 +---- app/models/service.rb | 19 ++----------------- app/validators/email_validator.rb | 3 ++- app/validators/service_area_validator.rb | 10 ++++++++++ app/validators/url_validator.rb | 1 + .../forms/_admin_email_fields.html.haml | 2 +- .../locations/forms/_admin_emails.html.haml | 4 ++-- .../locations/forms/_description.html.haml | 2 +- .../admin/locations/forms/_emails.html.haml | 2 +- .../locations/forms/_location_name.html.haml | 6 ++++-- .../locations/forms/_short_desc.html.haml | 2 +- .../admin/locations/forms/_urls.html.haml | 4 +++- .../admin/organizations/forms/_name.html.haml | 6 ++++-- .../admin/organizations/forms/_urls.html.haml | 4 +++- .../services/forms/_description.html.haml | 2 +- .../admin/services/forms/_name.html.haml | 6 ++++-- .../services/forms/_service_areas.html.haml | 4 +++- .../admin/services/forms/_urls.html.haml | 4 +++- config/locales/en.yml | 1 + spec/api/patch_service_spec.rb | 2 +- .../locations/update_admin_emails_spec.rb | 15 ++++++++++++++- .../admin/locations/update_emails_spec.rb | 13 +++++++++++++ .../admin/locations/update_name_spec.rb | 1 + .../admin/locations/update_urls_spec.rb | 13 +++++++++++++ .../admin/organizations/update_urls_spec.rb | 13 +++++++++++++ .../services/update_service_areas_spec.rb | 16 +++++++++++++++- .../admin/services/update_urls_spec.rb | 14 ++++++++++++++ spec/models/service_spec.rb | 6 +----- 31 files changed, 155 insertions(+), 56 deletions(-) create mode 100644 app/validators/service_area_validator.rb diff --git a/app/assets/stylesheets/application.css.scss b/app/assets/stylesheets/application.css.scss index 428180ad1..e70203c6e 100644 --- a/app/assets/stylesheets/application.css.scss +++ b/app/assets/stylesheets/application.css.scss @@ -92,6 +92,16 @@ margin: 10px 0px 15px 10px; } +// Adds a red border around invalid input fields after form submission. +.field_with_errors +{ + input, textarea + { + border-color: red; + border-width: 2px; + } +} + .accessibility { input { margin-top: 4px; } diff --git a/app/helpers/admin/form_helper.rb b/app/helpers/admin/form_helper.rb index 16579c3d9..4097adc9d 100644 --- a/app/helpers/admin/form_helper.rb +++ b/app/helpers/admin/form_helper.rb @@ -33,5 +33,14 @@ def nested_categories(categories) end end.join.html_safe end + + def error_class_for(model, attribute, field) + return if model.errors[attribute].blank? + 'field_with_errors' if field_contains_errors?(model, attribute, field) + end + + def field_contains_errors?(model, attribute, field) + model.errors[attribute].select { |error| error.include?(field) }.present? + end end end diff --git a/app/models/location.rb b/app/models/location.rb index 33ff58773..5e0dee548 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -67,15 +67,9 @@ class Location < ActiveRecord::Base # For example, the urls field is an array that can contain multiple URLs. # To be able to validate each URL in the array, we have to use a # custom array validator. See app/validators/array_validator.rb - validates :urls, array: { - format: { with: %r{\Ahttps?://([^\s:@]+:[^\s:@]*@)?[A-Za-z\d\-]+(\.[A-Za-z\d\-]+)+\.?(:\d{1,5})?([\/?]\S*)?\z}i, - message: "%{value} #{I18n.t('errors.messages.invalid_url')}", - allow_blank: true } } - - validates :emails, :admin_emails, array: { - format: { with: /\A([^@\s]+)@((?:(?!-)[-a-z0-9]+(?