Skip to content

Fix: address validation fires unconditionally when updating name/email on Admin User page#1423

Draft
ardalis with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-address-validation
Draft

Fix: address validation fires unconditionally when updating name/email on Admin User page#1423
ardalis with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-address-validation

Conversation

Copilot AI commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Submitting the Admin User personal info form with only name/email changes fails with {"InvalidUserId":["Bad Data"],"UserPersonalUpdateModel.Address":["The Address field is required."]} because address fields carry [Required] regardless of intent.

Changes

UserPersonalUpdateModel

  • Remove [Required] from Address, City, Country, PostalCode
  • Implement IValidatableObject: address group validation fires only when at least one address field is non-empty; if any is set, all of Address/City/Country/PostalCode must be provided
  • Add HasAnyAddressField() helper (reused in handler)
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
    if (!HasAnyAddressField()) yield break;

    if (string.IsNullOrWhiteSpace(Address))
        yield return new ValidationResult("The Address field is required when providing address information.", new[] { nameof(Address) });
    // ... City, Country, PostalCode
}

Admin/User.cshtml.csOnPostUpdatePersonalInfoAsync

  • Guard UpdateShippingAddress call behind HasAnyAddressField() to avoid null-dereference when no address is submitted

Admin/User.cshtml

  • Add asp-validation-summary to the personal info form so server-side validation errors render on-page instead of as a raw JSON BadRequest

Tests

  • Unit tests covering: no address fields → no errors; full address → no errors; partial address → errors on missing fields; whitespace-only treated as empty

- Remove [Required] from Address, City, Country, PostalCode fields in UserPersonalUpdateModel
- Implement IValidatableObject with conditional validation: if any address field is provided, all required address fields (Address, City, Country, PostalCode) must be filled
- Add HasAnyAddressField() helper method
- Fix OnPostUpdatePersonalInfoAsync to only call UpdateShippingAddress when address fields are present
- Add validation summary div to Admin/User.cshtml personal info form
- Add unit tests for HasAnyAddressField and conditional address validation

Closes #1422
Copilot AI changed the title [WIP] Fix address validation when updating user details Fix: address validation fires unconditionally when updating name/email on Admin User page Jul 16, 2026
Copilot AI requested a review from ardalis July 16, 2026 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Updating a user to try to set their name and email and the address validation fires

2 participants