admin管理员组文章数量:1130349
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.
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.
本文标签: custom post typesACF From amp To Date Validations
版权声明:本文标题:custom post types - ACF From & To Date Validations 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749239195a2337791.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论