Please provide example for custom sanitization of nested subgroup fields

Support MB Group Please provide example for custom sanitization of nested subgroup fieldsResolved

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #38436
    Prabakaran ShankarPrabakaran Shankar
    Participant

    In the custom sanitization callback, how to get the data from nested subgroups. Because when I use wp_kses to sanitize the nested subgroup type: textarea, it results "PHP Fatal error: Uncaught TypeError: preg_match(): Argument #2 ($subject) must be of type string".
    Do I need to unserialize the nested subgroup data and serialize it again before returning $value?

    #38442
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Please follow this documentation to know how to create a sanitization callback for a subfield in a group, the sub-subfield in a subgroup will work like that https://docs.metabox.io/sanitization/#sanitizing-subfields-in-a-group

    #38445
    Prabakaran ShankarPrabakaran Shankar
    Participant

    Hi, thank you for the reference.

    
    add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
        $meta_boxes[] = [
            'title'  => 'Multi-level nested groups',
            'fields' => [
                [
                    'id'     => 'group',
                    'type'   => 'group',
                     'sanitize_callback' => 'my_subfield_validation',
                    'fields' => [
                        [
                            'name' => 'Text',
                            'id'   => 'text',
                        ],
                        [
                            'name'   => 'Sub group',
                            'id'     => 'sub_group',
                            'type'   => 'group',
                            'fields' => [
                                // Normal field (cloned)
                                [
                                    'name'  => 'Sub text1',
                                    'id'    => 'sub_text1',
    'type'=> 'text',
                                ],
    [
                                    'name'  => 'Sub text2',
                                    'id'    => 'sub_text2',
    'type'=> 'textarea',
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ];
        return $meta_boxes;
    } );
    function my_subfield_validation( $value ) {
    // this is for fields    
    if ( empty( $value['text'] ) ) {
            $value['text'] = 'Hello World!';
        }
    // this is for sub-group
    $sub_group = isset($value['sub_group'])? $value['sub_group']:[];
    $tags = array();
    if(!empty($sub_group)){
     $value['sub_group'] = wp_kses($value['sub_group'], $tags);
    }
    //above condition results "PHP Fatal error: Uncaught TypeError: preg_match(): Argument #2 ($subject) must be of type string"
        return $value;
    }
    

    how to pass sub-group value to sanitize

    #38463
    Long NguyenLong Nguyen
    Moderator

    Hi,

    If you want to sanitize a custom field in a subgroup, for example sub_text1, please change the code to this one

    if( !empty( $sub_group ) ){
        $value['sub_group']['sub_text1'] = wp_kses_post( $sub_group['sub_text1'] );
    }

    $value['sub_group'] is not a string to sanitize so the error message will appear. Please read more on the documentation https://developer.wordpress.org/reference/functions/wp_kses/
    https://developer.wordpress.org/reference/functions/wp_kses_post/

    #38479
    Prabakaran ShankarPrabakaran Shankar
    Participant

    Hi, thank you for the above example. Could you please provide example for cloneable? For example, consider 'sub_text1' is the id of cloneable textarea field in the subgroup.

    #38494
    Long NguyenLong Nguyen
    Moderator

    Hi,

    If the field sub_text1 is a cloneable field, so the value is an array of values. You can sanitize a specific element of the array via the key. For example

    $sub_group = isset( $value['sub_group'] ) ? $value['sub_group'] : [];
    $sub_text1 = $sub_group['sub_text1'];
    if( !empty( $sub_text1 ) && is_array( $sub_text1 ) ){
        $value['sub_group']['sub_text1'][0] = wp_kses_post( $sub_text1[0] );
    }

    or this code if you want to sanitize all elements in the array

    if( !empty( $sub_text1 ) && is_array( $sub_text1 ) ) {
        foreach ($sub_text1 as $key => $text_value) {
            $value['sub_group']['sub_text1'][$key] = wp_kses_post( $text_value );
        }
    }
    #38496
    Prabakaran ShankarPrabakaran Shankar
    Participant

    Thank you so much.

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.