Problem with santize_callback function

Support MB Blocks Problem with santize_callback functionResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #23478
    DANIEL FITIPALDO R.DANIEL FITIPALDO R.
    Participant

    Hi,
    I have a text field in a block and set a callback_function in order to format the input data but it is not working.
    What am I doing wrong?
    This is how I defined the block with the amount text field:

    add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
    
    function your_prefix_register_meta_boxes( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = [
            'title'    => esc_html__( 'Test BLCK', 'text-domain' ),
            'id'       => 'test-blck',
            'fields'   => [
                [
                    'id'   => $prefix . 'text_hdsmywqrchf',
                    'type' => 'text',
                    'name' => esc_html__( 'Text Field Test BLCK', 'text-domain' ),
                ],
                [
                    'id'         => $prefix . 'taxonomy_advanced_yhwon6kcqb',
                    'type'       => 'taxonomy_advanced',
                    'name'       => esc_html__( 'Taxonomy Advanced Field', 'text-domain' ),
                    'taxonomy'   => 'test-type',
                    'field_type' => 'select',
                ],
                [
                    'id'                => $prefix . 'text_ey3ewpvlke',
                    'type'              => 'text',
                    'name'              => esc_html__( 'Amount text field', 'text-domain' ),
                    'size'              => 16,
                    'columns'           => 12,
                    'sanitize_callback' => 'xn_sanitize_money_field',
                ],
            ],
            'category' => 'layout',
            'icon'     => 'admin-appearance',
            'type'     => 'block',
            'mode'     => 'edit',
        ];
    
        return $meta_boxes;
    }

    This is my callback function defined in my functions.php:

    function xn_sanitize_money_field ($value) {
            // Do something with $value.
            $number = floatval ( $value );
            $value_new = number_format ($number, 2, ",", ".");  
            return $value_new;
    }

    And this is my problem:
    https://share.getcloudapp.com/OAuWJv9m

    #23481
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The sanitize callback function only works with normal custom fields, not in a block for now. I'm going to create a feature request for the developer team to support this case.

    Thank you.

    #23498
    DANIEL FITIPALDO R.DANIEL FITIPALDO R.
    Participant

    Ok. Thank you. What do you recommend me to do now, use js? Do you have any example that you can provide me with how I can solve this problem with javascript code.

    #23501
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can format the number input with a few lines of JavaScript code:

    jQuery(document).ready(function($) {
        $('body').on('keyup', '#field_ID', function() {
            var n = parseInt($(this).val().replace(/\D/g,''),10);
            $(this).val(n.toLocaleString("en-US"));
        })
    });

    Refer to this topic from StackOverFlow https://stackoverflow.com/questions/149055/how-to-format-numbers-as-currency-string

    #23502
    DANIEL FITIPALDO R.DANIEL FITIPALDO R.
    Participant

    Thank you Long, great support! you can close the ticket as resolved.

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