The allow null for a taxonomy select field does not seem to be working. This is for a multisite install. I have “Allow Null” in the field settings set to ‘yes’, and also have it set as a ‘required” field. It does not allow a non selection and It gives a validation error and pointing to the taxonomy field saying that it needs a value.
Required means that null is not allowed even if you set allow null to true. So it’s working the way I would expect it to work. This is the case with all fields that have the “allow null” value since requiring a value implicitly means that it cannot be null. Yep, a little confusing. The “Required” field setting is a setting on every field in ACF.
Thanks for the explanation @hube2 – Yeah it is definitely confusing. I have contacted the support team and put in a feature request for this.
So my scenario is I have the opportunity for a user to select a taxonomy to assign to a post, but only if taxonomies exist for that post type. If taxonomies do exist I need to have them required to select one of them.
Any direction for this?
Support led me in the right direction – I added a custom validation to that field, works like a charm
//**********Custom Validation for Taxonomy Field
add_filter('acf/validate_value/type=taxonomy', 'my_acf_validate_taxonomy', 10, 4);
function my_acf_validate_taxonomy( $valid, $value, $field, $input ){
// bail early if value is already invalid
if( !$valid ) {
return $valid;
}
$terms = get_terms( array(
'taxonomy' => $field['taxonomy'],
'hide_empty' => false,
) );
if( count($terms) > 0 ) {
if(!$value) {
$valid = 'Please select a category';
}
}
// return
return $valid;
}
If I’d had a chance to look at your first reply before you got the answer I would have offered a very similar solution. Glad you got it worked out.