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');
}
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Add 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');
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0
