1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.