zhuravljov / yii2-calc-validator
Math expression validator for Yii2
Installs: 63
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
This package is auto-updated.
Last update: 2024-12-19 01:43:44 UTC
README
Math expression validator without using eval()
for Yii2.
Installation
The preferred way to install this extension is through composer.
Either run
$ composer require zhuravljov/yii2-calc-validator
or add
"zhuravljov/yii2-calc-validator": "*"
to the require
section of your composer.json
file.
Usage
public function rules() { return [ // ... // Rule for checking an expression ['amount1', 'zhuravljov\yii\validators\CalcValidator'], // Rule for checking an expressions with saving result of calculation ['amount2', 'zhuravljov\yii\validators\CalcValidator', 'resultAttribute' => 'amount2'], // ... ]; }
After adding this validator, expressions can be used in forms.
For example:
12.80 + 3.50 * 2
- valid expression. Result:19.8
.(2 + 2) * 2
- valid expression. Result:8
.(2 + 2
- invalid expression.
This validator checks an expression and saves a result. And it can be used for currency fields or any numeric fields.
More details about calculation of expressions.