I have figured out what’s wrong:
$startDateObj = get_field_object('start_date', false, false, false) ?? null;
$key = $startDateObj['key'] ?? null;
$startDate = $_POST['acf'][$key] ?? null;
if(empty($startDate)) {
return $valid;
}
On the ajax call, it will be return as valid because get_field_object() returns false, therefor $key
and $startDate
will both have value null
(not undefined index) and in the end empty($startDate)
will be true. Which means it will pass the Ajax validation but, will fail when it runs the PHP validation because get_field_object()
will not return false.
Is there a way to fetch fields structure without relying on existing post? To have a list of static keys is not a good solution is in this case and I woludn’t want to add a custom javascript validation on top of AFC.
There is no PHP errors and wp_debug is enabled. If I run this:
<?php
$startDateObj = get_field_object('start_date', false, false, false);
echo 'Obj<br>';
var_dump($startDateObj);
$key = $startDateObj['key'] ?? null;
echo 'Key<br>';
var_dump($key);
$startDate = $_POST['acf'][$key] ?? null;
echo 'StartDate<br>';
var_dump($_POST['acf'][$key]);
exit;
Results in this:
Obj
array(22) {
[“ID”]=>
int(0)
[“key”]=>
string(19) “field_5a44cac20461f”
[“label”]=>
string(10) “Start date”
[“name”]=>
string(10) “start_date”
[“prefix”]=>
string(3) “acf”
[“type”]=>
string(16) “date_time_picker”
[“value”]=>
NULL
[“menu_order”]=>
int(1)
[“instructions”]=>
string(0) “”
[“required”]=>
int(1)
[“id”]=>
string(0) “”
[“class”]=>
string(0) “”
[“conditional_logic”]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(3) {
[“field”]=>
string(19) “field_5b33474da2033”
[“operator”]=>
string(2) “==”
[“value”]=>
string(5) “theme”
}
}
}
[“parent”]=>
string(19) “group_5a44bc8cad892”
[“wrapper”]=>
array(3) {
[“width”]=>
string(2) “50”
[“class”]=>
string(0) “”
[“id”]=>
string(0) “”
}
[“_name”]=>
string(10) “start_date”
[“_prepare”]=>
int(0)
[“_valid”]=>
int(1)
[“admin_only”]=>
int(0)
[“display_format”]=>
string(12) “F j, Y H:i:s”
[“return_format”]=>
string(11) “Y-m-d H:i:s”
[“first_day”]=>
int(1)
}Key
string(19) “field_5a44cac20461f”StartDate
string(19) “2018-10-09 00:00:00”
I’m updating an existing post.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.