This code is some I wrote to try and enforce data constraints for DECIMAL(m,n) column format, since Yii treats them as string. It does NOT support scientific / "E" notation format, for example:
-0.5, supported
-5e-1, NOT supported
Requirements ¶
Only tested in Yii 1.1.14 and 1.1.15, but should work for other versions of Yii 1.x
Usage ¶
I have DecimalValidator.php in components/validators/ folder, ensuring autoloading in config/main.php:
// autoloading model and component classes
'import'=>array(
/* ... */
'application.components.validators.*',
),
Then to use it for a field, like so:
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
// DECIMAL(7,2); 7 digits max length, 2 precision. allows sign (+ or -) by default
array('adjust_fee', 'DecimalValidator', 'm'=>7, 'n' => 2),
// DECIMAL(7,2); 7 digits max length, 2 precision. do not want them to put in sign
array('amount_paid', 'DecimalValidator', 'm'=>7, 'n' => 2, 'want_sign' => false),
);
}
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.