Yii 2 Money

Latest Stable Version Latest Unstable Version Total Downloads Monthly Downloads Daily Downloads
Thankful to Krajee!
to get more out of us.

NOTE: This extension depends on the kartik-v/yii2-krajee-base extension which in turn depends on the yiisoft/yii2-bootstrap extension. Check the composer.json for this extension's requirements and dependencies that may be updated by composer.

An advanced money mask input for Yii Framework 2 based on jQuery-maskMoney plugin. The plugin offers a simple way to create masks to your currency form fields. The yii2-money extension includes these additional enhancements in order to use the maskMoney plugin effectively:
  • default styling for Bootstrap 3 and supports Yii Active Field validations
  • automatically read a float/decimal and convert it to the money format on field load
  • automatically convert back the field to a float/decimal for saving once the mask is changed (maintains an internal hidden field)
  • allows global settings of the mask money plugin options via Yii::$app->params

The preferred way to install this extension is through composer. Either run:

$ php composer.phar require kartik-v/yii2-money "*"

or add:

"kartik-v/yii2-money": "*"

to the require section of your composer.json file. Then run:

php composer.phar update

to get the updated package on your application install.

The MaskMoney widget supports all the parameters similar to the \yii\widgets\InputWidget widget. The following additional properties are important for the plugin configuration:
  • disabled: boolean whether the input widget is to be entirely disabled. Defaults to false.

  • readonly: boolean whether the input widget is to be entirely readonly. Defaults to false.

  • options: array the HTML attributes for the input tag.

  • pluginOptions: array the JQuery plugin options for the jQuery-maskMoney plugin. You can control many options for the plugin. Refer the plugin options documentation for details.

You can globally set the plugin options for your money format across the application in the params section of your Yii configuration file. You need to set the maskMoneyOptions in your Yii application params. For example:

'params' => [
    'maskMoneyOptions' => [
        'prefix' => 'US$ ',
        'suffix' => '',
        'affixesStay' => true,
        'thousands' => ',',
        'decimal' => '.',
        'precision' => 2, 
        'allowZero' => false,
        'allowNegative' => false,
    ]
]

If you have not setup params like above, the plugin will default the thousandSeparator and decimalSeparator from Yii::$app->formatter settings in your configuration file. For example

'components' => [
    'formatter' => [
        'class' => 'yii\i18n\formatter',
        'thousandSeparator' => ',',
        'decimalSeparator' => '.',
    ]
]

Money mask widget with ActiveForm and model validation rule (amounts between 1 to 100000). Initial value is set to 1400.50. Note the prefix and suffix settings..

Money mask widget without ActiveForm or model and settings defaulted from maskMoneyOptions in Yii::$app->params.


A disabled MaskMoney input.

use kartik\money\MaskMoney;

// Money mask widget with ActiveForm and model validation rule (amounts between 1 to 100000). 
// Initial value is set to 1400.50. Note the prefix and suffix settings.
echo $form->field($model, 'amount')->widget(MaskMoney::classname(), [
    'pluginOptions' => [
        'prefix' => '$ ',
        'suffix' => ' €',
        'allowNegative' => false
    ]
]);

// Money mask widget without ActiveForm or model and settings defaulted from 
// `maskMoneyOptions` in `Yii::$app->params`
echo MaskMoney::widget([
    'name' => 'amount_drcr',
    'value' => 20322.22
]);

// SETTINGS in Yii::$app->params
'maskMoneyOptions' => [
    'prefix' => html_entity_decode('₹ '), // the Indian Rupee Symbol
    'suffix' => '', 
    'affixesStay' => true,
    'thousands' => ',',
    'decimal' => '.',
    'precision' => 2, 
    'allowZero' => true,
    'allowNegative' => true,
]

// A disabled MaskMoney input.
echo MaskMoney::widget([
    'name' => 'amount_1',
    'value' => 28239.35,
    'disabled' => true
]);

yii2-money is released under the BSD 3-Clause License. See the bundled LICENSE.md for details.