tha*_*smt 4 validation activerecord yii
我正在使用mySql DECIMAL(12,4)列来保存价格值(看看Magento使用的是什么).我想使用Yii的CValidator规则在我的ActiveRecord模型中验证它们,但我不太清楚如何做到这一点.
我假设我可以将CTypeValidator设置为"float",但我想看看其他人是如何做到这一点的.我没有看到实际的"货币"验证器.也许我应该只验证长度?
array('price', 'type', 'type'=>'float'),
Run Code Online (Sandbox Code Playgroud)
要么
array('price', 'length', 'max'=>17), // 12 + 4 + . = 17?
Run Code Online (Sandbox Code Playgroud)
建议和例子?谢谢!
ZaQ*_*ZaQ 10
我自己用过:
array('price', 'type', 'type'=>'float'),
Run Code Online (Sandbox Code Playgroud)
规则...如果需要,您还可以使用或组合前一个与"匹配"验证器
array('price', 'match', 'pattern'=>'fancy regex magic here'),
Run Code Online (Sandbox Code Playgroud)
添加一个工作示例,因为这是一个常见的问题(在SO中没有好的答案)"
public function rules(){
...
array('price', 'match', 'pattern'=>'/^[0-9]{1,12}(\.[0-9]{0,4})?$/'),
...
Run Code Online (Sandbox Code Playgroud)
其中{1,12}是整数的范围,{0,4}是"子"单位的范围.
对于0.01到9999.99的正常价格范围,使用这样的正则表达式:
'/^[0-9]{1,0}(\.[0-9]{0,2})?$/'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12118 次 |
| 最近记录: |