Problem with santize_callback function
- This topic has 4 replies, 2 voices, and was last updated 4 years, 4 months ago by
DANIEL FITIPALDO R..
-
AuthorPosts
-
December 9, 2020 at 5:25 AM #23478
DANIEL FITIPALDO R.
ParticipantHi,
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/OAuWJv9mDecember 9, 2020 at 9:40 AM #23481Long Nguyen
ModeratorHi,
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.
December 9, 2020 at 6:51 PM #23498DANIEL FITIPALDO R.
ParticipantOk. 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.
December 9, 2020 at 10:26 PM #23501Long Nguyen
ModeratorHi,
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
December 9, 2020 at 10:53 PM #23502DANIEL FITIPALDO R.
ParticipantThank you Long, great support! you can close the ticket as resolved.
-
AuthorPosts
- You must be logged in to reply to this topic.