$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom post types - ACF From & To Date Validations|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom post types - ACF From & To Date Validations

matteradmin11PV0评论

I have a custom post type 'event' having categories 'conference', 'seminars', and 'meetings' which have a custom field start_date and end_date to define a date for the event.

When one of the Event categories is selected from the right in Admin, a different set of custom fields are displayed as details required for conference/seminar/meetings are different.

I need to validate if the end_date is a date after start_date. Currently, end_date can be entered as a date before the start_date in the Admin.

So, I need to validate the end_date > start_date. I did a search for the code and have it below but not sure why it does not work. Maybe I'm not defining the custom post_type to trigger the validations or even defining event category in the below code.

add_action('acf/validate_save_post', 'my_acf_validate_save_post', 10, 0);

function my_acf_validate_save_post()
{


    $start = $_POST['fields']['field_5adb2e8fc30c3'];
   $start = new DateTime($start);

     $end = $_POST['fields']['field_5adb2ea4c30c4'];
    $end = new DateTime($end);



    // check custom $_POST data
        if ($start > $end) {
            acf_add_validation_error('end_date', 'End Date should be greater than or Equal to Start Date');
        }

}

When I click on the Update/Publish button when adding a new Event, there is NO alert message to warn - End date should be after the Start date.

How do I need to define to trigger this validation for Event custom post_type and also to show alert in all the three event category (Conference/Seminar/Meetings)?

I'm using ACF ver. 4.4.11.

Post a comment

comment list (0)

  1. No comments so far