Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    If I’m not mistaken, for the default checkbox stuff we have in CMB2, it’s either an empty value or “on” for a value. Are you doing something similar to that for values?

    Thread Starter Angelo Rocha

    (@angelorocha)

    Yes:

        $value          = wp_parse_args( $value, array(
            'id'        => '',
            'label'     => '',
            'type'      => 'text',
            'options'   => '',
            'size'      => '',
            'required'  => 'on' // this is checkbox
        ) );

    But when i deselect the checkbox, the field remains checked after save post.

    Thread Starter Angelo Rocha

    (@angelorocha)

    My solution, this is not best solution, but solved =(

    Sanitize checkbox:

            if ( empty($val['required']) ):
                unset( $val['required'] );
                $val['required'] = 'off';
            else:
                $val['required'] = 'on';
            endif;

    In my js:

    jQuery(function ($){
       $(".form_field_required input[type='checkbox']").each(function (){
          if($(this).val() !== 'on'){
              $(this).removeAttr('checked');
          }
       });
    });
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Sanitize custom field checkbox option’ is closed to new replies.