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:

displayInputName

string the name attribute for the masked display input. If not set this will be auto generated. One can set it to an empty string to prevent receiving the display input data as POST keys via form submission.

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 plugin options for the mask money jquery plugin.

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. Initial value is set to 20322.22.


Example of money mask widget for a different locale using comma , as the decimal separator and dot . as the thousands separator. Initial value is set to 0.01.


Money mask widget with zero precision along with prefix and suffix.


A disabled MaskMoney input.


Money mask widget with a default placeholder and allowEmpty set to true.

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`.  Initial value is set to 20322.22. 
echo MaskMoney::widget([
    'name' => 'amount_drcr',
    'value' => 20322.22
]);

// Example of money mask widget for a different locale using comma `,` as the decimal separator and 
// dot `.` as the thousands separator. Initial value is set to `0.01`.
echo MaskMoney::widget([
    'name' => 'amount_german',
    'value' => 0.01,
    'pluginOptions' => [
        'prefix' => '€ ',
        'thousands' => '.',
        'decimal' => ',',
        'precision' => 2
    ],
]);

// 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,
]

// Money mask widget with zero precision along with prefix and suffix.
echo MaskMoney::widget([
    'name' => 'amount_rounded_1',
    'value' => 1000,
    'pluginOptions' => [
        'prefix' => '$ ',
        'suffix' => ' €',
        'precision' => 0
    ]
]);

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

// Money mask widget with a default placeholder and `allowEmpty` set to `true`.
echo MaskMoney::widget([
    'name' => 'amount_ph_1',
    'value' => null,
    'options' => [
        'placeholder' => 'Enter a valid amount...'
    ],
    'pluginOptions' => [
        'allowZero' => false,
        'allowEmpty' => true
    ]
]);

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