skip to Main Content

My clone field "Services" has "Fields" set as "All fields from Section:services field group". My field group "Section: services" contains several fields including one called "Title" which I’ve set as required.

Now in my clone field "Services" I want "Title" to NOT be required. Problem is, if I try to make it not required in "Section:services", this update doesn’t extend to the "Services" clone field, where "Title" is still required.

Once a field group is cloned, is there not any way to changes those fields inside the cloned field?

EDIT:

My "Section: Services" field group containing the "Title" field whose required status I wanna change in the clone field:

enter image description here

My "Services" clone field containing "All fields from Section: Services field group" (it is within this clone field that I want my "Title" field to NOT be required any more):

enter image description here

2

Answers


  1. You can achieve this programmatically.

    In the example i am going to provide you with i have created two Field groups.

    One called Services containing the field Title named title and the other called Clone Services where on the Fields i have selected All fields from Services field group, Display: Seamless (replace this field with selected fields) and enabled the option Prefix Field Names. As a result the Title field in the Cloned group is named clone_title. You can replace the clone and title parts with the actual names of your fields.

    Now on its simple form you would need two hooks which can be placed in a custom plugin or in your functions.php file of your active theme. Please note that if you are not using a child theme, when you update your theme these would be overwritten and you would need to re-apply them.

    // Removes the required indication when the field is rendered in the backend.
    add_filter( 'acf/pre_render_field', function ( $field, $pid ) {
        if ( 'clone_title' === $field['name'] ) {
            $field['required'] = 0;
        }
        return $field;
    }, 10, 2 );
    
    // make the fields value to be accepted always, no matter what user actually inputed!
    add_filter( 'acf/validate_value/name=clone_title', function ( $valid, 
        $value, $field, $input ) {
        return true;
    }, 10, 4 );
    

    With these 2 hooks you can achieve what you are looking for but on its simplest form. You could evolve much more both of these 2 hooks to perform much more sophisticated checks and rules depending on your needs.

    Hope that helps!

    Login or Signup to reply.
  2. "I want to do that only for one particular field"

    Since you want to apply that modification only for a specific field, then you would need a unique value by which you would be able to target that specific field.

    "ACF" assigns a unique key, for each field, called "Field Key". We could use this value to target any specific field that we would like to! All we need to find out this value, for any field, is to use Screen Options and check the Field Keys checkbox, like the following screenshot:

    enter image description here

    Now we can target that specific field like this:

    add_filter("acf/pre_render_field", "ruvee_changing_required_field", 10, 2);
    
    function ruvee_changing_required_field($fields, $post_id)
    {
        // DO NOT forget to change this value to the unique value for your field  
        if ("field_64c2e81235358" == $fields["key"]) {
            $fields["required"] = 0;
        }
        return $fields;
    }  
    
    // DO NOT forget to change this value to the unique value for your field
    add_filter("acf/validate_value/key=field_64c2e81235358", "ruvee_changing_validation_rules", 10, 4);
    
    function ruvee_changing_validation_rules($valid, $value, $field, $input)
    {
        return true;
    }
    

    Tested and works seamlessly on wordpress Version 6.2 and ACF Version 6.1.7!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search