Support

Account

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

Solving

Make fields required only on front-end form?

  • I realize that, when using a front-end form, I can use other forms of validation for ACF fields. But before I do that, I’m wondering if there’s a way to do this within ACF.

    I have custom fields saving user meta and I’d like to require certain fields when filling out the front-end form, but I don’t want those fields to be required on the back end. So that, if someone is changing their password or updating their nickname or whatever, and they haven’t yet filled out these extra profile fields, they don’t get validation errors.

    (Running ACF Pro 5.7.9)

  • I am looking for this functionality as well. Has anyone come up with a good solution since?

    The form changes regularly and has conditional questions that are required if they are shown – so I’d like to avoid custom scripts to do it. My client also doesn’t have the budget to pay me to do that.

    Would there be a way to disable the required fields on the backend?

  • use https://www.advancedcustomfields.com/resources/acf-prepare_field/ see options there

    
    add_filter('acf/prepare_field', 'required_on_front');
    required_on_front($field) {
      if (!is_admin()) {
        $field['required'] = true,
      }
      return $field;
    }
    
    
  • Thanks for the snippet.

    I made two changes. One was to add the ‘function’, and the second was to change it so that no Admin fields are required vs making all Front End fields required.

    add_filter('acf/prepare_field', 'not_required_in_admin');
            function not_required_in_admin($field) {
                if (is_admin()) {
                    $field['required'] = false;
                    }
            return $field;
    }
  • Since this was the first hit in my searching, the correct answer is acf/load_field as described here: https://support.advancedcustomfields.com/forums/topic/setting-required-to-false-inside-acfprepare_field-doesnt-have-any-effect/.

  • Actually scratch that, acf_load_field is too powerful and actually prevents you from setting the fields to be required anywhere.

    My solution was to set the fields to be NOT required and then make them required only on the frontend form, like so:

    add_filter('acf/load_field/key=field_XYZ', 'custom_field_required_fields');
    function custom_field_required_fields($field) {
      if (!is_admin()) {
        $field['required'] = true;
      }
      return $field;
    }
  • Discovering now that these solutions cause problems with validation messages. The required attribute gets set on the frontend field but since the validation technically happens in the “admin” – custom messages are not applied and we just see the default browser message…

  • 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;
    }
Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Make fields required only on front-end form?’ is closed to new replies.