Support

Account

Home Forums General Issues Required field based on input other fields Reply To: Required field based on input other fields

  • 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;
    }