custom sanitization - cloneable group - php error

Support MB Group custom sanitization - cloneable group - php error

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

    Hello,
    The sanitization function of cloneable group throws a PHP error and there is no action.
    details are below.
    Metabox

    
    <?php
    add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
    
    function your_prefix_function_name( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = [
            'title'        => __( 'List1', 'your-text-domain' ),
            'id'           => 'list1',
            'post_types'   => ['page'],
            'storage_type' => 'custom_table',
            'table'        => 'custom',
            'fields'       => [
                [
                    'name'              => __( 'List', 'your-text-domain' ),
                    'id'                => $prefix . 'list_data',
                    'type'              => 'group',
                    'clone'             => true,
                    'sort_clone'        => true,
                    'add_button'        => __( 'Add more job', 'your-text-domain' ),
                    'sanitize_callback' => 'list_sanitize',
                    'fields'            => [
                        [
                            'name'     => __( 'Title', 'your-text-domain' ),
                            'id'       => $prefix . 'jtitle',
                            'type'     => 'text',
                            'required' => true,
                            'columns'  => 6,
                        ],
                        [
                            'name'     => __( 'Job Page_URL', 'your-text-domain' ),
                            'id'       => $prefix . 'jurl',
                            'type'     => 'url',
                            'required' => true,
                            'columns'  => 6,
                        ],
                        [
                            'name' => __( 'Description', 'your-text-domain' ),
                            'id'   => $prefix . 'shdp',
                            'type' => 'textarea',
                        ],
                        [
                            'name'     => __( 'Deadline Date', 'your-text-domain' ),
                            'id'       => $prefix . 'dedte',
                            'type'     => 'date',
                            'required' => true,
                            'columns'  => 4,
                        ],
                        [
                            'name'     => __( 'Subject', 'your-text-domain' ),
                            'id'       => $prefix . 'subli',
                            'type'     => 'select_advanced',
                            'options'  => [
                                'chemistry'     => __( 'Chemistry', 'your-text-domain' ),
                                'physics'       => __( 'Physics', 'your-text-domain' ),
                                
                            ],
                            'required' => true,
                            'columns'  => 4,
                        ],
                        [
                            'name'     => __( 'Added/Updated Date', 'your-text-domain' ),
                            'id'       => $prefix . 'aupd',
                            'type'     => 'date',
                            'required' => true,
                            'columns'  => 4,
                        ],
                    ],
                ],
            ],
        ];
    
        return $meta_boxes;
    }
    

    sanitize function

    
    function list_sanitize($value){
        //list - group clone
        
            $jtitle = isset( $value['jtitle'] ) ? $value['jtitle'] :[]; //title
            if( !empty($jtit) ){
                $value['jtitle'][0] = sanitize_text_field($jtitle[0]); // PHP error 1 - Only the first byte will be assigned to the string offset
            }
            $jurl = isset( $value['jurl'] ) ? $value['jurl'] :[]; //url
            if( !empty($joburl) ){
                $value['jurl'][0] = esc_url_raw($jurl[0]);
            }
            $shdp = isset( $value['shdp'] ) ? $value['shdp'] :[]; //short description
            if( !empty($shdp) ){
                $value['shdp'][0] = sanitize_textarea($shdp[0]); //PHP error 2 - Only the first byte will be assigned to the string offset
            }
            $dedte = isset($value['dedte'] ) ? $value['dedte'] :[]; // deadline date
            if( $dedte[0] <= date('Y-m-d') ){
                $value['dedte'][0] = '0000-00-00';
            }
        
        return $value;
        
    }
    

    PHP error
    1. Only the first byte will be assigned to the string offset
    2. Only the first byte will be assigned to the string offset

    #39508
    PeterPeter
    Moderator

    Hello,

    The cloneable is set in the group level so I think you will need to use a for loop to get the group item and check the subfield value inside the loop. For example:

    function list_sanitize($values){
        //list - group clone
        foreach ( $values as $key => $value ) {
            $jtitle = isset( $value['jtitle'] ) ? $value['jtitle'] :[]; //title
            if( !empty($jtit) ){
                $values[$key]['jtitle'] = sanitize_text_field($jtitle);
            }
        }
        
        return $values;
    }

    Let me know how it goes.

    #39524
    Prabakaran ShankarPrabakaran Shankar
    Participant

    Thank Mr. Peter,
    But no response for the code. I have checked as $values[$key]['jtitle'] = 'Hello world';

    #39577
    Prabakaran ShankarPrabakaran Shankar
    Participant

    Hello Peter,
    I believe the issue is still with the values from the [group clone] $values. I simply used the database data that was unserialized and serialized using your technique, and everything went smoothly. I believe the issue is values being passed to the sanitize function. Note: It only applies to clone values.

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