Skip to content

Fix: Allow empty values in optional email fields#7483

Closed
faisalahammad wants to merge 1 commit intopods-framework:release/3.3.5from
faisalahammad:fix/issue-7476-optional-email-field-validation
Closed

Fix: Allow empty values in optional email fields#7483
faisalahammad wants to merge 1 commit intopods-framework:release/3.3.5from
faisalahammad:fix/issue-7476-optional-email-field-validation

Conversation

@faisalahammad
Copy link
Contributor

@faisalahammad faisalahammad commented Jan 30, 2026

What this PR does

This pull request fixes issue #7476 where optional email fields (fields not marked as required) were incorrectly showing validation errors when left empty. When a user tried to save a post with an empty optional email field, they would see "Invalid email address format" error and then "Please complete these fields before saving: Email" message, preventing them from saving the content.

Problem Description

The JavaScript email validator was using a condition that treated empty strings as invalid values. The validation logic if ( ! value || ! value.match( EMAIL_REGEX ) ) meant that if a value was falsy (empty string, null, undefined) OR didn't match the email regex, it would throw an error. This meant optional fields could never be saved empty.

Solution

Updated the emailValidator function in ui/js/dfv/src/helpers/validators.js to only validate non-empty values:

Before:

if ( ! value || ! value.match( EMAIL_REGEX ) ) {
    throw __( 'Invalid email address format.', 'pods' );
}

After:

// Allow empty values for optional fields (required validation is handled separately).
if ( value && ! value.match( EMAIL_REGEX ) ) {
    throw __( 'Invalid email address format.', 'pods' );
}

This change ensures that:

  • Empty values pass through validation for optional fields
  • Non-empty values are still validated for proper email format
  • Required field validation continues to work as expected
  • Invalid email formats still show appropriate error messages

Changes Made

  • Modified: ui/js/dfv/src/helpers/validators.js - Updated emailValidator function
  • Built: ui/js/dfv/pods-dfv.min.js - Rebuilt minified JavaScript
  • Updated: ui/js/dfv/pods-dfv.min.asset.json - Updated asset dependencies

Testing Steps

I have thoroughly tested this fix with the following scenarios:

Test Case 1: Optional Email Field - Empty Value

  1. Create a new Pod with an email field
  2. Set the field as "Not Required" (optional)
  3. Create a new post and leave the email field empty
  4. Save/update the post
    Result: Post saves successfully without any errors

Test Case 2: Optional Email Field - Valid Email

  1. Edit the same post
  2. Enter a valid email address: test@example.com
  3. Save/update the post
    Result: Post saves successfully

Test Case 3: Optional Email Field - Remove Email

  1. Edit the post with existing email
  2. Remove the email address (make field empty)
  3. Save/update the post
    Result: Post saves successfully without errors (this was previously broken)

Test Case 4: Optional Email Field - Invalid Email Format

  1. Edit the same post
  2. Enter an invalid email: not-an-email
  3. Attempt to save
    Result: Shows "Invalid email address format" error (expected behavior)

Test Case 5: Required Email Field - Empty Value

  1. Create a new Pod with email field marked as "Required"
  2. Create a post and leave email empty
  3. Attempt to save
    Result: Shows "Email is required" error (expected behavior works correctly)

Test Case 6: Required Email Field - Valid Email

  1. Enter valid email in required field
  2. Save the post
    Result: Saves successfully (works as expected)

Environment Tested

  • WordPress version: 6.9
  • Pods version: 3.3.4
  • PHP version: 8.3.30
  • Browser: Chrome latest

Additional Notes

The PHP-side validation in classes/fields/email.php was already handling empty values correctly. The issue was purely in the JavaScript client-side validation. This fix aligns the JavaScript validation behavior with the PHP validation logic.

Required field validation is handled separately through other validation mechanisms, so this change only affects the email format validation portion.

Checklist

Related Issue

Fixes #7476

Screen recording

https://videos.faisalahammad.com/recordings/8O6tgvIXigVMg4EdXbta

Fixes issue pods-framework#7476 where optional email fields (not marked as required)
were incorrectly showing validation errors when left empty.

The JavaScript email validator was treating empty strings as invalid
values, causing the "Invalid email address format" error even for
optional fields. This commit updates the validation logic to only
validate non-empty values, allowing optional fields to be saved
with empty email addresses.

Changes:
- Modified emailValidator function to check if value exists before
  validating format
- Rebuilt minified JavaScript assets

Testing:
- Created optional email field (not required)
- Added valid email address and saved successfully
- Removed email address (made empty) and saved without errors
- Verified required email fields still work correctly
- Tested with invalid email format still shows proper error
@what-the-diff
Copy link

what-the-diff bot commented Jan 30, 2026

PR Summary

  • Updated Pod Version
    The version value in a key file (pods-dfv.min.asset.json) has been updated to a new version (022eb858b9b3dada4e4d). This might involve new updates or bug fixes enhancing the software efficiency.

  • Modified Email Validation Function
    The function that checks if an entered email is valid (emailValidator in validators.js) has been modified. This update now allows for optional email fields to be left empty, enhancing user experience by not forcing them to provide an email when not necessary.

@sc0ttkclark sc0ttkclark changed the base branch from main to release/3.3.5 February 22, 2026 05:03
@sc0ttkclark sc0ttkclark added this to the Pods 3.3.5 milestone Feb 22, 2026
@sc0ttkclark
Copy link
Member

Merged manually via 4a70872

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to save an empty email address when the field is marked as optional

2 participants