Support

Account

Home Forums Front-end Issues Make fields required only on front-end form? Reply To: Make fields required only on front-end form?

  • I think I figured this out, it requires 2 filters and leverages the “acf_form” variable which is passed by the front-end form on validation.

    1)

    // Allow empty values in admin (verify acf_screen == acf_form because this is set in the frontend form but not the admin
    // and both are technically is_admin() since the action takes place in the backend
    add_filter('acf/validate_value/key=field_123', 'custom_vehicle_submission_required_fields', 10, 5);
    function custom_vehicle_submission_required_fields($valid, $value, $field, $input) {
      
      if (is_admin() && acf_request_arg( '_acf_screen', false ) !== 'acf_form') {
        // valid is set to false if the value is empty, but allow 0 as a valid value
        if ( empty( $value ) && ! is_numeric( $value ) ) {
          $valid = true;
        }
      }
      return $valid;
    }

    2)

    // Do not add the 'required' attribute on the field in the admin
    add_filter('acf/prepare_field/key=field_123', 'custom_vehicle_submission_required_fields2');
    function custom_vehicle_submission_required_fields2($field) {
      
      if (is_admin()) {
        $field['required'] = false;
      }
      return $field;
    }