Yii2 MaskedInput

I want to have a field that allow numeric only, group by thousands and no decimal is allowed (user can not type the ‘.’)

Is there a way to do that with Yii2 MaskedInput (http://www.yiiframework.com/doc-2.0/yii-widgets-maskedinput.html ) ?




<?= $form->field($model, 'from_date')->widget(\yii\widgets\MaskedInput::className(), [

    'mask' => '99999999'



the text input field also underlines which I want to remove.

If user enter 123456789, the input field should show 123,456,789.

If user try to enter decimal point, the input field should not allow it.

I tried Kartik MaskMoney which the jquery maskmoney itself has a bug when I set precision to 0. So I am looking for other options.




<?=

$form->field($model, 'from_date')->widget(\yii\widgets\MaskedInput::className(), [

	'clientOptions' => [

    	'alias' => 'decimal',

    	'groupSeparator' => ',',

    	'autoGroup' => true

	]

]);

?>



Yii uses a wrapper of this Masked Input Widget

You can use additional options: removeMaskOnSubmit, this option will remove the mask before submitting the form.




<?=

$form->field($model, 'from_date')->widget(\yii\widgets\MaskedInput::className(), [

	'clientOptions' => [

    	'alias' => 'decimal',

    	'groupSeparator' => ',',

    	'autoGroup' => true,

        'removeMaskOnSubmit' => true

	]

]);

?>



But, my problem is:

When user type 123456789, input field show: 123,456,789

When user type 123456789.55, input field show: 123,456,789.55

When user submit form, only comma been removed, and POST value is: 123456789.55

Can you help me solve this problem?

You can try the following however, i don’t know if it will work




<?=

$form->field($model, 'from_date')->widget(\yii\widgets\MaskedInput::className(), [

'clientOptions' => [

'alias' => 'numeric',

'allowMinus'=>false,

'groupSize'=>3,

'radixPoint'=> ".",

'groupSeparator' => ',',

'autoGroup' => true,

'removeMaskOnSubmit' => true

//'unmaskAsNumber'=>true//dont know what this does

]

]);

?>



you can see all off the undocumented options here they start at line 70 and go to 95

If the above doesn’t work then you can try one of these three options.

  1. You will have to write your own custom masked input mask (look at docs to see how)

  2. Implement a before save function that strips the "." from the field




public function beforeSave($insert) {

//preform on both create and update

//removes commas and periods

$model->from_date = str_replace('.', '', $value);

//removes just periods  

//$model->from_date =str_replace(['.', ','], '' , $value);

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

        	return true;

    	} else {

        	return false;

    	}

	}



  1. Create a rule to strip it.



//removes commas and periods

['from_date', 'filter', 'filter' => function($value) { 	return str_replace(['.', ','], '' , $value); }]

removes just periods

['from_date', 'filter', 'filter' => function($value) { 	return str_replace('.', '' , $value); }]



however, the rule may cause other validation issues. I’d personally write my own or try and find an option in the code that lets you strip it on save.


<?=

$form->field($model, 'from_date')->widget(\yii\widgets\MaskedInput::className(), [

        'clientOptions' => [

            ...

            'removeMaskOnSubmit' => true,

            'placeholder' => '',

            ...

        ]

]);

?>

Just add ‘placeholder’ => ‘’,