admin管理员组文章数量:1305747
I have two date fields where I want to make sure the "end date" is always later than the "start date".
I added a validation function per ACF's documentation. It does fire, but the custom message does not display next to the field. It only says "validation failed" on the top.
add_action('acf/validate_save_post', 'my_acf_validate_save_post');
function my_acf_validate_save_post()
{
$start = $_POST['acf']['field_5fb0e816ea4fc'];
$start = new DateTime($start);
$end = $_POST['acf']['field_5fb0e83aea4fd'];
$end = new DateTime($end);
// check custom $_POST data
if ($start > $end) {
acf_add_validation_error('event_series_end_date', 'End Date should be later than the Start Date');
}
}
I have two date fields where I want to make sure the "end date" is always later than the "start date".
I added a validation function per ACF's documentation. It does fire, but the custom message does not display next to the field. It only says "validation failed" on the top.
add_action('acf/validate_save_post', 'my_acf_validate_save_post');
function my_acf_validate_save_post()
{
$start = $_POST['acf']['field_5fb0e816ea4fc'];
$start = new DateTime($start);
$end = $_POST['acf']['field_5fb0e83aea4fd'];
$end = new DateTime($end);
// check custom $_POST data
if ($start > $end) {
acf_add_validation_error('event_series_end_date', 'End Date should be later than the Start Date');
}
}
Share
Improve this question
asked Jan 25, 2021 at 3:04
geochantogeochanto
13115 bronze badges
1 Answer
Reset to default 1Add this snippet and check according to your needed.
add_action('acf/validate_save_post', 'my_acf_validate_save_post');
function my_acf_validate_save_post()
{
$start = $_POST['acf']['field_5fb0e816ea4fc'];
//$start = new DateTime($start);
$start = strtotime($start);
$end = $_POST['acf']['field_5fb0e83aea4fd'];
//$end = new DateTime($end);
$end = strtotime($end);
if( current_user_can('manage_options') ) {
acf_reset_validation_errors();
}
// check custom $_POST data
if ($start > $end ) {
acf_add_validation_error($_POST['acf']['field-600e609de8ab8'], 'End Date should be later than the Start Date');
}else if ($start == $end ) {
acf_add_validation_error($_POST['acf']['field-600e609de8ab8'], 'End Date should be equal to the Start Date');
}
}
Screenshot in Error :
本文标签: ACF Custom validation message not showing up
版权声明:本文标题:ACF Custom validation message not showing up 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741807052a2398566.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论