Yii2 Number Validation Comma And Dot As Decimal Seperator

Hello in our country people use comma’s and dots as a decimal seperator.

1,50 as well as 1.50

Is there a way instead of getting notified that 1,50 isn’t a number,

to change the comma in a dot.

By key press up (jQuery), or before getting notified that the input isn’t a number.

I’m using the Basic template, please tell me where and which files to adjust or override.

Thanks in advance,

Greetings from Holland!

You can adjust the NumberValidator::$numberPattern property to include comma or dot:

[['myAttribute'], 'number', 'numberPattern' => '/^\s*[-+]?[0-9]*[.,]?[0-9]+([eE][-+]?[0-9]+)?\s*$/'],


Have you considered using a spinner rather than an input element. A spinner would eliminate the problem. Take a look at the Kartik-v/Yii2-Widgets extension.

That looks good, but where do I put my "before update" code to transform the comma to a dot before saving to my database?

A very good idea! Thanks I’ll look into it as well…

Got it, I’ve adjusted the rules of my model

public function rules() {

        return [

            [['prod_name', 'prod_price'], 'required'],

            [['prod_price'], 'number', 'numberPattern' => '/^\s*[-+]?[0-9]*[.,]?[0-9]+([eE][-+]?[0-9]+)?\s*$/'],

            [['prod_name'], 'string', 'max' => 40],

            [['prod_name'], 'unique']



And added the beforeSave to my model

public function beforeSave($insert) {

        if (parent::beforeSave($insert)) {

            $this->prod_price = str_replace(",", ".", $this->prod_price);

            return true;

        } else {

            return false;