Home › Forums › General Issues › Required field based on input other fields
Hi,
I need a situation in which a certain field is ONLY required when other fields are not empty. In this particular case the field ‘Title’ is not required. However when one of the following fields are not empty it has to be required: Deal or Discount, Description, Regular Price, Discounted price / deal price and Valid until. I need this situation because either all fields need to be empty or all field need to be filled.
This is the json code I created so far:
[
{
"key": "group_deal1",
"title": "Deal 1 test 2 (displayed on homepage)",
"fields": [
{
"key": "field_deal1title",
"label": "Title",
"name": "deal_title_one_test1",
"type": "text",
"instructions": "(max. 100 characters)",
"required": [
[
{
"field": "field_deal1dealdiscount",
"operator": "!=empty"
}
],
[
{
"field": "field_deal1description",
"operator": "!=empty"
}
],
[
{
"field": "field_deal1regularprice",
"operator": "!=empty"
}
],
[
{
"field": "field_deal1discountedprice",
"operator": "!=empty"
}
],
[
{
"field": "field_deal1validdate",
"operator": "!=empty"
}
]
],
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "deals-title",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": 100
},
{
"key": "field_deal1dealdiscount",
"label": "Deal or Discount",
"name": "discount_deal_or_special_one",
"type": "radio",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"choices": {
"Discount": "Discount",
"Deal": "Deal"
},
"allow_null": 1,
"other_choice": 0,
"default_value": "",
"layout": "vertical",
"return_format": "value",
"save_other_choice": 0
},
{
"key": "field_deal1description",
"label": "Description",
"name": "deal_description_one",
"type": "textarea",
"instructions": "(max. 600 characters)",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "deals-description",
"id": ""
},
"default_value": "",
"placeholder": "",
"maxlength": 600,
"rows": "",
"new_lines": ""
},
{
"key": "field_deal1regularprice",
"label": "Regular price",
"name": "regular_price_one",
"type": "number",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "deals-regular-price",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "$",
"append": "",
"min": "",
"max": "",
"step": ""
},
{
"key": "field_deal1discountedprice",
"label": "Discounted price \/ deal price",
"name": "discounted_price_one",
"type": "number",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "deals-discounted-price",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "$",
"append": "",
"min": "",
"max": "",
"step": ""
},
{
"key": "field_deal1validdate",
"label": "Valid until",
"name": "valid_until_one",
"type": "date_picker",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"display_format": "m\/d\/Y",
"return_format": "m\/d\/Y",
"first_day": 1
}
],
"location": [
[
{
"param": "post_type",
"operator": "==",
"value": "post"
},
{
"param": "post_format",
"operator": "==",
"value": "aside"
}
],
[
{
"param": "post_format",
"operator": "==",
"value": "status"
}
]
],
"menu_order": 1,
"position": "acf_after_title",
"style": "default",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": 1,
"description": ""
}
]
It doen’t work.. It gives me a result in which the field Title is required at all times.
Hopefully someone can help. Thank you in advance!
PS. I know how to hide fields with conditional logic but that doesn’t empty the fields and therefor will still be visible on the website. So another solution would be to empty the fields when the Title is empty.
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;
}
Hi John, this is great! Thank you!
Now I need to add the other conditions (Description, Regular Price, etc.). And it needs to be ‘and/or’. Would the code become something like this?
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
}
// title is required if field_deal1description has a value
elseif (!empty($_POST['acf']['field_deal1description'])) {
// 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;
}
I would probably build one large if condition
if (!empty($_POST['acf']['field_1') ||
!empty($_POST['acf']['field_2') ||
!empty($_POST['acf']['field_3') {
// check the value
}
but that would depend on how complicated it got.
Hmmm… Your code gave me errors so I changed it to this but that didn’t work:
add_filter('acf/validate_value/key=field_5c66e017f8359', 'custom_validate_title', 20, 4);
function custom_validate_title($valid, $value, $field, $input) {
// title is required if other fields has a value
if (!empty($_POST['acf']['field_5c6d43781bcb1']))
if (!empty($_POST['acf']['field_5c66e094f835a']))
if (!empty($_POST['acf']['field_5c66e0d4f835b']))
if (!empty($_POST['acf']['field_5c66e130f835c']))
if (!empty($_POST['acf']['field_5c703c1fd5851'])) {
// the other fields 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;
}
I just fiddled with it a bit and I think this should be it?
add_filter('acf/validate_value/key=field_5c66e017f8359', 'custom_validate_title', 20, 4);
function custom_validate_title($valid, $value, $field, $input) {
// title is required if other fields has a value
if (!empty($_POST['acf']['field_5c6d43781bcb1']));
if (!empty($_POST['acf']['field_5c66e094f835a']));
if (!empty($_POST['acf']['field_5c66e0d4f835b']));
if (!empty($_POST['acf']['field_5c66e130f835c']));
if (!empty($_POST['acf']['field_5c703c1fd5851'])); {
// the other fields 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;
}
John, maybe you can help me one more time with a code for the Regular price which is only required when Deal or Discount (tickbox) its value is Discount. Maybe something like this?
add_filter('acf/validate_value/key=field_5c66e0d4f835b', 'custom_validate_deal1regularprice', 20, 4);
function custom_validate_deal1regularprice($valid, $value, $field, $input) {
// Discounted price is required if other fields has a value
if (!empty($_POST['acf']['field_5c6d43781bcb1'])); {
// If the other fields has a value 1
// Check value of this field;
if ($value = 1 ) {
$valid = 'This field is required because you have chosen for a deal with <strong>Discount</strong>. Either fill in a <strong>Regular price</strong> below or change your deal to a deal without discount.';
}
}
return $valid;
}
In my first example I was missing a ) at then of if
add_filter('acf/validate_value/key=field_5c66e017f8359', 'custom_validate_title', 20, 4);
function custom_validate_title($valid, $value, $field, $input) {
// title is required if other fields has a value
if (!empty($_POST['acf']['field_5c6d43781bcb1']) ||
if (!empty($_POST['acf']['field_5c66e094f835a']) ||
if (!empty($_POST['acf']['field_5c66e0d4f835b']) ||
if (!empty($_POST['acf']['field_5c66e130f835c']) ||
if (!empty($_POST['acf']['field_5c703c1fd5851'])) {
// the other fields 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;
}
if requires ==
, also, no ; before the {
add_filter('acf/validate_value/key=field_5c66e0d4f835b', 'custom_validate_deal1regularprice', 20, 4);
function custom_validate_deal1regularprice($valid, $value, $field, $input) {
// Discounted price is required if other fields has a value
if (!empty($_POST['acf']['field_5c6d43781bcb1'])) {
// If the other fields has a value 1
// Check value of this field;
if ($value == 1 ) {
$valid = 'This field is required because you have chosen for a deal with <strong>Discount</strong>. Either fill in a <strong>Regular price</strong> below or change your deal to a deal without discount.';
}
}
return $valid;
}
I tried this but that also doesn’t work:
add_filter('acf/validate_value/key=field_5c66e017f8359', 'custom_validate_title', 20, 4);
function custom_validate_title($valid, $value, $field, $input) {
if (!empty($_POST['acf']['field_5c6d43781bcb1']) ||
!empty($_POST['acf']['field_5c66e094f835a']) ||
!empty($_POST['acf']['field_5c66e0d4f835b']) ||
!empty($_POST['acf']['field_5c66e130f835c']) ||
!empty($_POST['acf']['field_5c703c1fd5851'])) {
if (empty($value)) {
$valid = 'This field is required because you have added a value to some other field';
}
}
return $valid;
}
add_filter('acf/validate_value/key=field_5c66e017f8359', 'custom_validate_title', 20, 4);
function custom_validate_title($valid, $value, $field, $input) {
// title is required if other fields has a value
if (!empty($_POST['acf']['field_5c6d43781bcb1']) ||
!empty($_POST['acf']['field_5c66e094f835a']) ||
!empty($_POST['acf']['field_5c66e0d4f835b']) ||
!empty($_POST['acf']['field_5c66e130f835c']) ||
!empty($_POST['acf']['field_5c703c1fd5851'])) {
// the other fields 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;
}
This is eventually the code for the Regular price being required when Deal or Discount is set to Discount (radio buttons):
add_filter('acf/validate_value/key=field_5c66e0d4f835b', 'custom_validate_regular_price_deal_one', 20, 4);
function custom_validate_regular_price_deal_one($valid, $value, $field, $input) {
// Regular price is required if Deal with or without discount has value With discount
if($_POST['acf']['field_5c7970b4aecf7'] == "Discount"){
// value of Deal with or without discount is With discount
// check value of this field;
if (empty($value)) {
$valid = 'This field is required because the field <strong>Deal with or without discount</strong> is set to <strong>With discount</strong>. Either fill in a <strong>Regular price</strong> or set your deal to <strong>Without discount</strong>.';
}
}
return $valid;
}
So all is solved! Thank you!
The topic ‘Required field based on input other fields’ is closed to new replies.
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.