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

Fix validation failures and error page display #16399

Merged
merged 4 commits into from
Jul 13, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions dashboard/app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def upgrade
successfully_updated = can_update && current_user.update(update_params(params_to_pass))
has_email = current_user.parent_email.blank? && current_user.hashed_email.present?
success_message_kind = has_email ? :personal_login_created_email : :personal_login_created_username
user.reload unless successfully_updated # if update fails, roll back user model so error page renders correctly
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the error page not inspect user.errors? Reloading user will clear those.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That doesn't appear to be the case, the errors show up correctly with this change:

screen shot 2017-07-13 at 1 17 23 pm

What the user typed in gets cleared out, which is not ideal, but in this context (just the upgrade call) it's not terrible because most of that information is either secret confirmation or password-related, so doesn't auto-fill anyways.

respond_to_account_update(successfully_updated, success_message_kind)
end

Expand Down
12 changes: 12 additions & 0 deletions dashboard/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ def district_teachers(cohort = nil)
validates_length_of :username, within: 5..20, allow_blank: true
validates_format_of :username, with: USERNAME_REGEX, on: :create, allow_blank: true
validates_uniqueness_of :username, allow_blank: true, case_sensitive: false, on: :create, if: 'errors.blank?'
validates_uniqueness_of :username, allow_blank: true, case_sensitive: false, on: :update, if: 'errors.blank? && username_changed?'
validates_presence_of :username, if: :username_required?
before_validation :generate_username, on: :create

Expand Down Expand Up @@ -472,6 +473,17 @@ def self.find_channel_owner(encrypted_channel_id)
validates :email, no_utf8mb4: true
validates_email_format_of :email, allow_blank: true, if: :email_changed?, unless: -> {email.to_s.utf8mb4?}
validate :email_and_hashed_email_must_be_unique, if: 'email_changed? || hashed_email_changed?'
validate :presence_of_hashed_email_or_parent_email, if: :requires_email?

def requires_email?
provider_changed? && provider.nil? && encrypted_password_changed? && encrypted_password.present?
end

def presence_of_hashed_email_or_parent_email
if hashed_email.blank? && parent_email.blank?
errors.add :email, I18n.t('activerecord.errors.messages.blank')
end
end

def presence_of_email
if email.blank?
Expand Down