Make WordPress Core

Opened 4 months ago

Last modified 3 months ago

#65281 new defect (bug)

Custom Time & Date Format Fields Allow Empty Save Instead of Validation

Reported by: ashirhabib Owned by:
Priority: normal Milestone: Awaiting Review
Component: Date/Time Version:
Severity: normal Keywords: has-patch has-unit-tests has-test-info needs-testing
Cc: Focuses: administration

Description

Title

Custom Time & Date Format Fields Allow Empty Save Instead of Validation

Description

During WordPress 7.0 release party testing, an issue was identified related to custom time and date format settings.

When the custom fields for Time Format and Date Format are cleared (left blank), WordPress still allows the settings to be saved successfully.

Although WordPress falls back to default formatting behavior internally, the system should validate these fields before saving.

Current Behavior

  • User clears the custom Time Format and/or Date Format fields.
  • WordPress accepts and saves the changes.
  • WordPress automatically falls back to default formatting behavior.

Expected Behavior

  • If custom Time Format or Date Format fields are empty or invalid: System should either save null values explicitly, OR Prevent saving and show a validation/error message.
  • User should be prompted to enter valid format values before settings are saved.

Suggested Validation

  • Restrict empty submissions for custom format fields.
  • Display an admin notice such as:

<blockquote>
Please configure a valid date and time format.
</blockquote>

Steps to Reproduce

# Go to Settings → General.

# Navigate to Date Format / Time Format settings.

# Clear the custom format fields.

# Click Save Changes.

# Observe that WordPress still saves the settings without validation.

Impact

  • May create confusion for users/admins.
  • Blank or invalid custom settings can lead to inconsistent formatting behavior.
  • No clear indication that configuration is incomplete.

Environment

  • WordPress Version: 7.0
  • Tested during: WordPress 7.0 Release Party Testing

Attachments (6)

Globl Date & Time Settings for WordPerss General.png (57.2 KB ) - added by ashirhabib 4 months ago.
Screenshot 2026-05-21 at 2.34.24 PM.png (228.1 KB ) - added by narenin 4 months ago.
Empty date format can be saved
Screenshot 2026-05-21 at 2.42.07 PM.png (185.7 KB ) - added by narenin 4 months ago.
Getting "Please configure a valid date and time format." message with patch
Date and time format issue fix.png (39.7 KB ) - added by sanayasir 4 months ago.
Save settings now gives null values on custom fields are empty.png (50.1 KB ) - added by sanayasir 4 months ago.
Screenshot 2026-06-06 180035.png (82.6 KB ) - added by sumitsingh 3 months ago.
tested on Playground using this link - https://playground.wordpress.net/wordpress.html?pr=11903

Download all attachments as: .zip

Change History (22)

#1 @sumitsingh
4 months ago

  • Focuses coding-standards added

I have tested and able to generate this issue. So we need to implement validation here.

This ticket was mentioned in PR #11903 on WordPress/wordpress-develop by @khokansardar.


4 months ago
#2

  • Keywords has-patch has-unit-tests added; needs-patch removed

Validate custom date and time format fields on the General Settings screen before saving. When either custom field is empty, restore the previous value, show an admin error, and reject empty values in sanitize_option() as well.

Fixes #65281.

#3 follow-ups: @westonruter
4 months ago

Adding client-side validation as well would be welcome.

#4 @narenin
4 months ago

@ashirhabib Thanks for the details I am also able to replicate this issue.

Environment

WordPress: 7.0
PHP: 8.1.23
Server: nginx/1.16.0
Database: mysqli (Server: 8.0.16 / Client: mysqlnd 8.1.23)
Browser: Chrome 126.0.0.0 (macOS)
Theme: Twenty TwentyVersion: 2.7

@narenin
4 months ago

Empty date format can be saved

#5 @narenin
4 months ago

Test Report

I have tested latest patch and it is working as expected.

https://github.com/WordPress/wordpress-develop/pull/11903

Environment

WordPress: 7.0-alpha-58576-src
PHP: 8.1.23
Server: nginx/1.16.0
Database: mysqli (Server: 8.0.16 / Client: mysqlnd 8.1.23)
Browser: Chrome 126.0.0.0 (macOS)
Theme: Twenty TwentyVersion: 2.7

Expected Results

Getting the message "Please configure a valid date and time format." when we try to save empty Date format using custom.

Last edited 4 months ago by narenin (previous) (diff)

@narenin
4 months ago

Getting "Please configure a valid date and time format." message with patch

#6 @narenin
4 months ago

  • Keywords has-test-info added

#7 @jorbin
4 months ago

  • Focuses coding-standards removed
  • Version 6.9.4

I tested on the oldest available version on playground (6.3) and this issue was present then, so it certainly wasn't introduced in 6.9.4. I've opened issue with the playground team to support even older versions of WordPress in the hope of helping identify when this started to be an issue.

#8 @sanayasir
4 months ago

I tested this issue on WordPress Playground 7.1 Alpha.

Previously, the General Settings page allowed saving the settings even when the custom Date Format and Time Format fields were left empty/null.

After testing the fix, the behavior now works as expected according to the ticket requirements:

When the custom date/time format fields are cleared, the settings are no longer saved.
A validation error message is displayed: “Please configure a valid date and time format.”
The issue appears to be fixed successfully.

#9 in reply to: ↑ 3 @khokansardar
4 months ago

Replying to westonruter:

Adding client-side validation as well would be welcome.

Thanks for the feedback @westonruter. Added client-side validation on the PR.

The General Settings form now blocks submission when Custom is selected and the date or time format field is empty. Invalid rows use the standard admin form-invalid styling, focus moves to the first invalid field, and the error is announced via wp.a11y.speak().

This builds on the server-side validation in the first commit. PHPUnit tests for sanitize_option() continue to pass.

Last edited 4 months ago by khokansardar (previous) (diff)

#10 in reply to: ↑ 3 @westonruter
4 months ago

Replying to westonruter:

Adding client-side validation as well would be welcome.

Here's a minimal patch which would address this:

  • src/wp-admin/options-general.php

    diff --git a/src/wp-admin/options-general.php b/src/wp-admin/options-general.php
    index 6a9a6d2f38..5c2bae8731 100644
    a b require_once ABSPATH . 'wp-admin/admin-header.php';  
    6767<div class="wrap">
    6868<h1><?php echo esc_html( $title ); ?></h1>
    6969
    70 <form method="post" action="options.php" novalidate="novalidate">
     70<form method="post" action="options.php">
    7171<?php settings_fields( 'general' ); ?>
    7272
    7373<table class="form-table" role="presentation">
    foreach ( $date_formats as $format ) {  
    518518                        /* translators: Hidden accessibility text. */
    519519                        __( 'Custom date format:' ) .
    520520                '</label>' .
    521                 '<input type="text" name="date_format_custom" id="date_format_custom" value="' . esc_attr( get_option( 'date_format' ) ) . '" class="small-text" />' .
     521                '<input type="text" name="date_format_custom" id="date_format_custom" value="' . esc_attr( get_option( 'date_format' ) ) . '" class="small-text" required />' .
    522522                '<br />' .
    523523                '<p><strong>' . __( 'Preview:' ) . '</strong> <span class="example">' . date_i18n( get_option( 'date_format' ) ) . '</span>' .
    524524                "<span class='spinner'></span>\n" . '</p>';
    foreach ( $time_formats as $format ) {  
    563563                        /* translators: Hidden accessibility text. */
    564564                        __( 'Custom time format:' ) .
    565565                '</label>' .
    566                 '<input type="text" name="time_format_custom" id="time_format_custom" value="' . esc_attr( get_option( 'time_format' ) ) . '" class="small-text" />' .
    567566                '<br />' .
     567                '<input type="text" name="time_format_custom" id="time_format_custom" value="' . esc_attr( get_option( 'time_format' ) ) . '" class="small-text" required />' .
    568568                '<p><strong>' . __( 'Preview:' ) . '</strong> <span class="example">' . date_i18n( get_option( 'time_format' ) ) . '</span>' .
    569569                "<span class='spinner'></span>\n" . '</p>';

No need for custom JS.

Related: The novalidate attribute was removed from the comments form in #47595.

#11 @khokansardar
4 months ago

Thanks @westonruter for the minimal patch suggestion — that's a much cleaner approach. I've updated the PR to drop the custom JS validation entirely and instead:

  • Removed novalidate="novalidate" from the <form> in options-general.php so the browser's built-in constraint validation fires.
  • Added required to the date_format_custom and time_format_custom inputs.

#12 @abdullahramzan
3 months ago

  • Keywords needs-testing added

This ticket was mentioned in Slack in #core by abdullahramzan. View the logs.


3 months ago

#14 @jadavsanjay
3 months ago

Test Report

I have tested latest patch and it is working as expected.

Patch tested: https://github.com/WordPress/wordpress-develop/pull/11903

Environment

  • WordPress: 7.1-alpha-20260529.035253
  • Subdirectory: No
  • PHP: 8.3.31
  • Server: PHP.wasm
  • Database: WP_SQLite_Driver (Server: 8.0.38 / Client: 3.51.0)
  • Browser: Chrome 148.0.0.0
  • OS: Windows 10/11
  • Theme: Twenty Twenty-Five 1.5
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.3.0

Actual Results

  1. ✅ Issue resolved with patch.

Additional Notes

  • After applying the patch, the issue with the Custom Time & Date Format fields allowing empty values has been resolved.

Screenshots/Screencast with results

Before Patch
https://i.postimg.cc/zLD3C8Z3/before-1.png
After Apply Patch
https://i.postimg.cc/LJ3ngh7L/after-patch-1.png

#15 @sumitsingh
3 months ago

I have tested on https://playground.wordpress.net/ and seems working fine for me. more information you can see mentioned screenshot.

#16 @darshitrajyaguru97
3 months ago

Test Report

I tested the latest patch version and confirmed that the reported issue has been fixed.

Patch tested: https://github.com/WordPress/wordpress-develop/pull/11903

Environment

  • WordPress: 7.1-alpha-20260529.035253
  • PHP: 8.3.31
  • Database: WP_SQLite_Driver
  • Browser: Chrome
  • OS: Windows 11
  • Theme: Twenty Twenty-Five
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.3.0

Steps taken

  • Applied the latest patch from the PR.
  • Tested the Custom Time & Date Format fields behavior.
  • Verified that empty values are no longer accepted incorrectly.

Result

  • Patch successfully resolves the reported issue.
  • Custom Time & Date Format fields now behave as expected after applying the patch.

Expected result

Custom Time & Date Format fields should properly prevent invalid empty values from being saved.

Note: See TracTickets for help on using tickets.