Home › Forums › ACF PRO › Start date and End date date-picker field validation › Reply To: Start date and End date date-picker field validation
i did resolved it. Now user can not choose the start date greater than end date. I simply add a function in my functions.php file.
// Date Picker Validation --- Start Date should be less than End Date.
add_action('acf/validate_save_post', 'my_acf_validate_save_post', 10, 0);
function my_acf_validate_save_post()
{
$start = $_POST['acf']['key of datepicker 1'];
$start = new DateTime($start);
$end = $_POST['acf']['key of datepicker 2'];
$end = new DateTime($end);
// check custom $_POST data
if ($start > $end) {
acf_add_validation_error('name of datepicker field you want to display error message. ', 'End Date should be greater than or Equal to Start Date');
}
}
In this i have select two datepicker and then converted them to date format then compared them as you can see above.
* You can get key of ACF field from sreen option and then selecting field keys
* You can select the name of the field by inspecting in chrome and selecting the datepicker input field.
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.