Comma-formatting in decimals in the editor
Comma-formatting in decimals in the editor
I'm trying to implement decimal number formatting by using the comma instead of the dot. The error occurs when the Editor is going to save the data.
In PHP file i have this instance
Field::inst( 'rawProducts.olii' )->validator( 'Validate::numeric')->getFormatter( 'Format::toDecimalChar' )->setFormatter( 'Format::fromDecimalChar')->setFormatter( 'Format::ifEmpty', 0 ),
The data is rendered correctly with the comma, but when I click the save button I return the following error
This input must be given as a number.
Specifically, in the Mysql table the field is defined as float (6.2)
I've tried several options like entering the comma option in validation, replacing the setFormatter method with this function
->setFormatter( function($val, $data, $field) {return str_replace ( ',' , '.' , $val );})
I would also need to include a validation of minimum / maximum value but with this the problems multiply .
I have tried for many hours to come up with this issue but without results, no suggestions. Thanks in advance
Answers
Hi,
The reason you aren't getting any where with the built in validators is that the validation function runs before the formatting function. So if you submit
3,141
that is what the numeric validator will see, and reject since it has a comma in it.The good news is that the built in numeric validator has an option to specify what the decimal character should be:
should do it.
Regards,
Allan
Hi Allan, thanks for your suggestions. I tried but did not work.
I get this error when I save the data
SQLSTATE[01000]: Warning: 1265 Data truncated for column 'olii' at row 1
You would kindly have some other idea to suggest. Thank you
Can you show me the exact code you are using please? Also you also use a formatter to convert from the comma to a period?
Allan
Hi Allan
This is the part of code where I create the field instance:
Field::inst( 'rawProducts.olii' )->getFormatter( 'Format::toDecimalChar')->setFormatter( 'Format::fromDecimalChar')->setFormatter( 'Format::ifEmpty', 0 )->validator( 'Validate::numeric', [ "decimal" => ','] ),
Here the Editor form part
{
label: "Olii g",
name: "rawProducts.olii"
},
And for the table this is the code:
{ data: "rawProducts.olii", className: "dt-body-center", render: $.fn.dataTable.render.number( '.', ',', 2 ) },
The field in the table is float type.
Thank you very much for your patience and support
This is probably the cause of the issue. Although you can have multiple validators, you cannot have multiple get or set formatters. You would need to create a custom formatter function that would do the formatting that you need - in this case checking it the value is empty and also converting the decimal point.
I'd suggest in the short term removing the
ifEmpty
formatter to see if that is the issue.Allan
Hi Allan
I solved this way
Instance field like this
And nothing else!!!!!
Thank you for your advice.
Please @Alan and @rf1234 1234, is there anything wrong with the code below and if there is, what is the right way to format numbers to fourdecimal places like 1000 = 1000.0000
Field::inst( 'nestor_exchangerates.exchangerate_amount' )
->validator( 'Validate::numeric', [
"decimal" => '.'
] ),
->getFormatter( function ($val) {return (number_format($val,4));})
->setFormatter( function($val,$data) {return number_format($val,4);}),
I see you are still working on your forex table
I have a little one myself too.
This is the PHP including the validators and formatters. The data base field is defined as DECIMAL(11,5).
As you can see I have get and set formatters for the rate. I have two languages with different formatting. You can pass the number of decimals you would like to have as a parameter. If you don't pass anything the number of decimals will be 4.
@mankramo , I forgot to mention it but I don't know whether anything is wrong with your code. If it's not working feel free to use bits and pieces of my code above which is working fine.
Thank you very much @rf1234 for your continued assistance, I really appreciate it . Your functions worked perfectly, i just took out those language conditions (My app is a mono-lang app for now )