Skip to content

Trim whitespace from address fields before database save#215

Merged
rhoerr merged 5 commits intomage-os:release/3.xfrom
ProxiBlue:bugfix/trim-address-fields-before-save
May 7, 2026
Merged

Trim whitespace from address fields before database save#215
rhoerr merged 5 commits intomage-os:release/3.xfrom
ProxiBlue:bugfix/trim-address-fields-before-save

Conversation

@ProxiBlue
Copy link
Copy Markdown
Contributor

Summary

  • Add trimAddressFields() to AbstractAddress::beforeSave() to trim leading/trailing whitespace (including Unicode) from all string address fields before persistence
  • Covers both checkout (Quote\Address) and customer address (Customer\Address) save paths since both extend AbstractAddress
  • Uses preg_replace with /u flag to also strip Unicode whitespace (non-breaking spaces, zero-width spaces)
  • Includes 9 unit tests covering all trimmed fields, street handling, Unicode whitespace, null safety, and edge cases

Fixes #214

Fields trimmed

firstname, lastname, middlename, prefix, suffix, company, city, region, telephone, fax, postcode, vat_id, email, and each line of the street field.

Test plan

  • Verify checkout (guest): enter address fields with leading/trailing spaces → confirm trimmed values in sales_order_address
  • Verify customer address add/edit: enter fields with spaces → confirm trimmed on save
  • Verify existing orders/addresses are unaffected (trim only runs on new saves)
  • Run unit tests: vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Magento/Customer/Test/Unit/Model/Address/AbstractAddressTest.php

@rhoerr
Copy link
Copy Markdown
Contributor

rhoerr commented Apr 5, 2026

Thanks for the PR

@rhoerr rhoerr added the bug Something isn't working label Apr 7, 2026
@rhoerr rhoerr changed the base branch from 2.4-develop to release/3.x April 24, 2026 02:35
@rhoerr rhoerr requested a review from a team as a code owner April 24, 2026 02:35
@rhoerr rhoerr changed the base branch from release/3.x to 2.4-develop April 24, 2026 02:36
@rhoerr
Copy link
Copy Markdown
Contributor

rhoerr commented Apr 24, 2026

Can you change the PR base to release/3.x for Mage-OS 3.0? You might need to remerge in the process.

Thanks

Comment thread app/code/Magento/Customer/Model/Address/AbstractAddress.php Outdated
Comment thread app/code/Magento/Customer/Model/Address/AbstractAddress.php
ProxiBlue and others added 5 commits April 24, 2026 19:51
Add trimAddressFields() to AbstractAddress::beforeSave() to trim
leading/trailing whitespace (including Unicode) from all string
address fields before persistence.

Fixes: mage-os#214

Fields trimmed: firstname, lastname, middlename, prefix, suffix,
company, city, region, telephone, fax, postcode, vat_id, email,
and each line of the street field.

Covers both checkout (Quote\Address) and customer address
(Customer\Address) save paths since both extend AbstractAddress.

empty
The beforeSave() tests call parent::beforeSave() which dispatches
events via _eventManager. The test setUp was missing an event manager
mock on the Context object, causing "Call to a member function
dispatch() on null" errors.

empty
- Replace getMockForAbstractClass() with createMock() for PHPUnit 12
  compatibility
- Expand unicodeTrim() regex to also strip zero-width space (U+200B),
  zero-width non-joiner (U+200C), zero-width joiner (U+200D), and
  byte order mark (U+FEFF) which are not matched by \s in PHP PCRE

empty
- Break unicodeTrim regex into variable to stay under 120 char line limit
- Use createMock() instead of getMockForAbstractClass() (removed in PHPUnit 12)

empty
So before-save observers see the trimmed values that will actually be persisted.

Addresses review feedback on PR mage-os#215.
@ProxiBlue ProxiBlue force-pushed the bugfix/trim-address-fields-before-save branch from bb4c779 to df843cc Compare April 24, 2026 11:52
@ProxiBlue ProxiBlue changed the base branch from 2.4-develop to release/3.x April 24, 2026 11:52
@rhoerr rhoerr merged commit 437d8e9 into mage-os:release/3.x May 7, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants