我有一个简单的表格,其中包括一个需要钱的输入,即浮动。到目前为止,它只能接受格式良好的十进制,例如12768.56。注入一些服务器端逻辑(这里没有javascript )的任务是拦截输入值,比如12 768,56,用12768.56替换它,然后让symfony/原则完成它的工作。转换只是一个例子,我得到了我需要的东西,但是问题是--,我应该把拦截器函数放在哪里?我想这应该是XxxForm.class.php里的一些东西。但我不知道是哪种方法。doSave?processData?我很确定有个特别的地方.
发布于 2015-03-02 10:21:56
您应该将这类逻辑放入自定义验证器中:
class myValidatorMoney extends sfValidatorNumber {
protected function doClean($value) {
$clean = $this->processNumber($value); // your logic in this function
if($clean === false) { // if not possible to process
throw new sfValidatorError($this, 'invalid', array('value' => $value));
}
return parent::doClean($clean);
}
}通过这种方式,它可以更好地处理symfony窗体,updateXXXColumn()可以处理有效值,但是对于无效的输入,没有什么可以做的。
发布于 2015-02-28 12:59:34
我找到了sfFormDoctrine类的源代码:http://trac.symfony-project.org/browser/branches/1.4/lib/plugins/sfDoctrinePlugin/lib/form/sfFormDoctrine.class.php。那里有一个片段:
153 /**
154 * Processes cleaned up values with user defined methods.
155 *
156 * To process a value before it is used by the updateObject() method,
157 * you need to define an updateXXXColumn() method where XXX is the PHP name
158 * of the column.
159 *
160 * The method must return the processed value or false to remove the value
161 * from the array of cleaned up values.
162 *
163 * @see sfFormObject
164 */
165 public function processValues($values)这就是说,我们应该在教义形式类中实现updateXXXColumn()方法。所以我做到了:
// lib/form/doctrine/XxxForm.class.php
+ protected function updateAmountColumn($value)
+ {
+ return Tools::processMoneyStrToFloat($value);
+ }效果很好。
https://stackoverflow.com/questions/28781335
复制相似问题