Home › Forums › General Issues › Required field based on input other fields › Reply To: Required field based on input other fields
You can’t do what you are attempting to do to make the field required. the `$field[‘required’] setting is Boolean value and if it is an array then checking it returns true.
Yes, conditional logic does not remove values from fields, it is up to us to check this condition in the template before we display the value of other fields, for example.
if (get_field('description')) {
// only show title when there is a description
?><h1><?php the_field('title'); ?></h1><?php
the_field('description');
}
If you want to have one field required based on another field then you can do this using custom validation rules https://www.advancedcustomfields.com/resources/acf-validate_value/, for example:
add_filter('acf/validate_value/key=field_deal1title', 'custom_validate_title', 20, 4);
function custom_validate_title($valid, $value, $field, $input) {
// title is required if field_deal1dealdiscount has a value
if (!empty($_POST['acf']['field_deal1dealdiscount'])) {
// the other field has a value
// check value of this field
if (empty($value)) {
$valid = 'This field is required because you have added a value to some other field';
}
}
return $valid;
}
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.