Fix server error on registration for wrong DOB (#40607)

This commit is contained in:
Sudeep Tarlekar authored and Claire committed 2026-09-30 17:43:49 +02:00
1 parent 56558f4b67
commit 79802c5556
2 files changed
+55

No files matched your search

@@ -29,6 +29,8 @@ class Auth::RegistrationsController < Devise::RegistrationsController
def create
super
rescue ActiveRecord::MultiparameterAssignmentErrors => e
handle_multiparameter_assignment_error(e.errors)
end
def update
@@ -109,6 +111,28 @@ class Auth::RegistrationsController < Devise::RegistrationsController
end
end
def handle_multiparameter_assignment_error(errors)
build_resource(sign_up_params_without_invalid_multiparameter_attributes(errors))
errors.each do |attribute_error|
resource.errors.add(attribute_error.attribute, :invalid)
end
clean_up_passwords resource
set_minimum_password_length
respond_with resource
end
def sign_up_params_without_invalid_multiparameter_attributes(errors)
invalid_attributes = errors.map { |attribute_error| attribute_error.attribute.to_s }
sign_up_params.to_h.reject do |key, _value|
invalid_attributes.any? do |attribute|
key.to_s == attribute || key.to_s.match?(/\A#{Regexp.escape(attribute)}\(\d+i\)\z/)
end
end
end
def determine_layout
%w(edit update).include?(action_name) ? 'admin' : 'auth'
end
@@ -283,6 +283,37 @@ RSpec.describe Auth::RegistrationsController do
end
end
context 'with an invalid date of birth' do
subject do
Setting.registrations_mode = 'open'
Setting.min_age = 16
post :create, params: {
user: {
:account_attributes => { username: 'test' },
:email => 'test@example.com',
:password => '12345678',
:password_confirmation => '12345678',
:agreement => 'true',
'date_of_birth(1i)' => '2019',
'date_of_birth(2i)' => '32',
'date_of_birth(3i)' => '01',
},
}
end
it 'responds with an error message about the date of birth' do
expect { subject }.to_not raise_error
expect(response).to have_http_status(:success)
expect(date_of_birth_error_text).to eq(I18n.t('errors.messages.invalid'))
expect(User.find_by(email: 'test@example.com')).to be_nil
end
def date_of_birth_error_text
response.parsed_body.css('.user_date_of_birth .error').text
end
end
it_behaves_like 'registration mode based responses', :create
end