Skip to content

[Bug] Input format configuration for "date" fields not working as expected. #1289

@datgausaigon

Description

@datgausaigon

My Environment:

  • PHP: 8.2.x
  • WordPress: 6.8.1 (Blank Site)
  • Theme: Twenty Twenty-Five 1.2
  • Carbon Fields: 3.6.8

My code:

use Carbon_Fields\Container;
use Carbon_Fields\Field;


add_action( 'admin_enqueue_scripts', function () {
	wp_enqueue_script( 'flatpickr-locale-vn', 'https://npmcdn.com/flatpickr/dist/l10n/vn.js',
		[ 'carbon-fields-core' ] );
}, 10000 );

add_action( 'carbon_fields_register_fields', 'crb_add_event_fields' );
function crb_add_event_fields(): void {
	$event_category = get_term_by( 'slug', 'event', 'category' );

	if ( $event_category ) {
		Container::make( 'post_meta', 'Event Details' )
		         ->where( 'post_type', '=', 'post' )
		         ->where( 'post_term', '=', [
			         'field'            => 'term_id',
			         'value'            => $event_category->term_id,
			         'taxonomy'         => 'category',
			         'include_children' => true
		         ] )
		         ->add_fields( [
			         Field::make( 'date', 'crb_event_date', 'Event Date' )
			              ->set_attribute( 'placeholder', __( 'Event Date' ) )
			              ->set_picker_options( [
				              'dateFormat' => 'Y/m/d',
				              'locale'     => 'vn',
			              ] )
			              ->set_input_format( 'Y/m/d', 'Y/m/d' )
			              ->set_storage_format( 'Y-m-d' )
			              ->set_required( false ),

			         Field::make( 'text', 'crb_event_location', 'Event Location' )
			              ->set_attribute( 'placeholder', __( 'Event Location' ) )
			              ->set_required( false )
		         ] );
	}

What I expect is that the input (and display) in admin for "date" field should be y/m/d as my source code. But "date" field only allows input (and display) according to "locale", if locale is default then it will be (M/d/y), if it is "vn" then it will be (d/M/y). Please see my attached screenshot.

With default "locale":

Image

With "vn" "locale":

Image

Is it because my code is not correct? If not, can you help me?

Thanks
🐻

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions