Validate comma as numeric in Editor

Validate comma as numeric in Editor

pmwebsoftpmwebsoft Posts: 1Questions: 1Answers: 0

Greetings,

I have searched for a solution to a problem i'm having regarding input in Editor.

I want to validate a field as numeric, but want to allow comma as a decimal separator instead the dot that is by default.

From the documentation i found this :
https://editor.datatables.net/manual/php/validation#Numbers

I performed the following code to try, but the numeric validation does not accept the comma as valid :

Field::inst( 'expenses.expenseValue' )
            ->validator( 'Validate::notEmpty', array('message'=> 'É necessário colocar um valor numérico!'))
            ->validator( 'Validate::numeric', array(
                'decimal'=> ',',
                'message'=> 'É necessário colocar um valor numérico!'
            )),

What am i doing wrong ? Does anyone has a working example so i can compare what is my mistake ?

Best Regards,
Pedro

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Hi Pedro,

    You need to use a set formatter as well to convert it to a decimal form the database will understand:

    Field::inst( 'expenses.expenseValue' )
                ->validator( 'Validate::notEmpty', array('message'=> 'É necessário colocar um valor numérico!'))
                ->validator( 'Validate::numeric', array(
                    'decimal'=> ',',
                    'message'=> 'É necessário colocar um valor numérico!'
                ))
                ->setFormatter( 'Format::fromDecimalChar', ',' ),
    

    Allan

  • williamay53williamay53 Posts: 7Questions: 5Answers: 0

    Hi Allan, do there is Javascript counterpart for the set formatter?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @williamay53 ,

    For the backend, you can use JS if you're using Node - see here.

    Cheers,

    Colin

This discussion has been closed.