Home › Forums › General Issues › How to use ACF datepicker for start date less than end date validation › Reply To: How to use ACF datepicker for start date less than end date validation
I’ve been playing with this issue and found a solution that works for me by compiling a few solutions I found.
add_filter('acf/validate_value/name=date_time_end', 'validate_end_date_func', 10, 4);
function validate_end_date_func($valid, $value, $field, $input) {
if (!$valid) {
return $valid;
}
$start_key = 'field_5f46d8cef3f0a';
$end_key = 'field_5f46d8eaf3f0b';
$start_value = $_POST['acf'][$start_key];
$start_value = new DateTime($start_value);
$end_value = $_POST['acf'][$end_key];
$end_value = new DateTime($end_value);
if ($end_value <= $start_value) {
$valid = 'The end date must come after the start date for this event.';
}
return $valid;
}
Like noted above:
* For the $start_key
and $end_key
You can get key of ACF field from screen option and then selecting field keys.
* In line 1 where name=date_time_end
set date_time_end
to your field name.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.