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!
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;
}
Hi John,
I’m still getting errors, even with the extra )
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;
}
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;
}
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;
}
Well, to be honest I don’t think this questions is ‘valid’ anymore. I thought I had to change the code of the field group and put it somewhere manually to make my other question work (https://support.advancedcustomfields.com/forums/topic/required-field-based-on-input-other-fields/#post-84436). But since you helped me with that amazingly well I can just retrieve the original field keys from the php export and add the filter you mentioned in the other question to my functions.php file. I tested it and it works. Now only need to add the other validation rules. Thanks a lot. I will mark this question as answered
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;
}
Hi John, you are truly helpful! I saw you also replied on my other question. Thanks for that!
So what would be the best way to go? I guess adding the php script (including the code to make certain fields required from my other question) to my functions.php file adding the location rule?
How do I add the location rule? Do I add it to if( function_exists('acf_add_local_field_group') ):
? What would be the code?
Sorry if this question might be stupid but my coding skills are not that great…
But I only want to use the field group on specific template files.
I’ve created a new php file acf-deals.php and uploaded it to the /php-includes/ directory in the root directory. It includes the following code:
<?php
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_deal1',
'title' => 'Deal One (displayed on homepage)',
'fields' => array(
array(
'key' => 'field_5c66e017f8359',
'label' => 'Title',
'name' => 'deal_title_one',
'type' => 'text',
'instructions' => '(max. 100 characters)',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => 'deals-title',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => 100,
),
array(
'key' => 'field_5c6d43781bcb1',
'label' => 'Deal or Discount',
'name' => 'discount_deal_or_special_one',
'type' => 'radio',
'instructions' => '',
'required' => 0,
'conditional_logic' => array(
array(
array(
'field' => 'field_5c66e017f8359',
'operator' => '!=empty',
),
),
),
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array(
'Discount' => 'Discount',
'Deal' => 'Deal',
),
'allow_null' => 1,
'other_choice' => 0,
'default_value' => '',
'layout' => 'vertical',
'return_format' => 'value',
'save_other_choice' => 0,
),
array(
'key' => 'field_5c66e094f835a',
'label' => 'Description',
'name' => 'deal_description_one',
'type' => 'textarea',
'instructions' => '(max. 600 characters)',
'required' => 0,
'conditional_logic' => array(
array(
array(
'field' => 'field_5c66e017f8359',
'operator' => '==empty',
),
),
),
'wrapper' => array(
'width' => '',
'class' => 'deals-description',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'maxlength' => 600,
'rows' => '',
'new_lines' => '',
),
array(
'key' => 'field_5c66e0d4f835b',
'label' => 'Regular price',
'name' => 'regular_price_one',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => 'deals-regular-price',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '$',
'append' => '',
'min' => '',
'max' => '',
'step' => '',
),
array(
'key' => 'field_5c66e130f835c',
'label' => 'Discounted price / deal price',
'name' => 'discounted_price_one',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => 'deals-discounted-price',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '$',
'append' => '',
'min' => '',
'max' => '',
'step' => '',
),
array(
'key' => 'field_5c703c1fd5851',
'label' => 'Valid until',
'name' => 'valid_until_one',
'type' => 'date_picker',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'display_format' => 'm/d/Y',
'return_format' => 'm/d/Y',
'first_day' => 1,
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
array(
'param' => 'post_format',
'operator' => '==',
'value' => 'aside',
),
),
array(
array(
'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' => '',
));
endif;
?>
I’ve added this code to my single-aside.php file:
<?php get_template_part( '/php-includes/acf-deals.php', 'acf-deals' ); ?>
Should that not work?
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.