FIX: Don't require fields required on sign-up when updating fields (#27888)

### What is the problem?

We have recently added a new option to add user fields required for existing users. This is in contrast to requiring fields only on sign-up.

This revealed an existing problem. Consider the following:

1. User A signs up.
2. Admin adds a new user field required on sign-up. (Should not apply to User A since they already signed up.)
3. User A tries to update their profile.

**Expected behaviour:**

No problem.

**Actual behaviour:**

User A receives an error saying they didn't fill up all required fields.

### How does this fix it?

When updating profile, we only check that required fields that are "for all users" are filled. Additionally, we check that fields that were required on sign-up and have previously been filled are not blanked out.
This commit is contained in:
Ted Johansson 2024-07-15 07:56:20 +08:00 committed by GitHub
parent 9e4e591d60
commit 06131bd4fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 1 deletions

View File

@ -220,7 +220,12 @@ class UsersController < ApplicationController
value = nil if value === "false"
value = value[0...UserField.max_length] if value
if value.blank? && field.required?
if value.blank? &&
(
field.for_all_users? ||
field.on_signup? &&
user.custom_fields["#{User::USER_FIELD_PREFIX}#{field_id}"].present?
)
return render_json_error(I18n.t("login.missing_user_field"))
end
attributes[:custom_fields]["#{User::USER_FIELD_PREFIX}#{field.id}"] = value

View File

@ -1528,6 +1528,20 @@ RSpec.describe UsersController do
end.not_to change { user1.reload.user_fields[field_id] }
end
it "value is required only on sign-up" do
user_field.on_signup!
expect do
put update_user_url, params: { user_fields: { field_id => "" } }
end.to change { user1.reload.user_fields[field_id] }.from(nil).to("")
put update_user_url, params: { user_fields: { field_id => valid_options } }
expect do
put update_user_url, params: { user_fields: { field_id => "" } }
end.not_to change { user1.reload.user_fields[field_id] }
end
it "value can nil or empty if the field is not required" do
put update_user_url, params: { user_fields: { field_id => valid_options } }