Remove comma before submit to DataTables

##

The comma problem from user input

Many cases user like to submit decimal or integer with common (,) and MySQL or MariaDB will return error like

An SQL error occurred: SQLSTATE[01000]: Warning: 1265 Data truncated for column 'max_amount' at row 1

The solution : replace before pass to database server

I’ve modified and make sure to support join tables that using dot notation

    editor.on( 'preSubmit', function ( e, data, action ) {
        function set(obj, str, val) {
            str = str.split(".");
            while (str.length > 1)
                obj = obj[str.shift()];
            return obj[str.shift()] = val;
        }
    
        function ref(obj, str) {
            return str.split(".").reduce(function(o, x) { return o[x] }, obj);
        }   

        $.each( data.data, function ( key, values ) {
            set(data.data[ key ], 'table_name.min_amount', ref(values, 'table_name.min_amount').replace(/,/g, ''));
            set(data.data[ key ], 'table_name.max_amount', ref(values, 'table_name.max_amount').replace(/,/g, ''));
        } );
    } );       

Tags:

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.