2

在使用 Lithium 框架开发 PHP 应用程序时,我注意到零值没有显示在表单的文本字段中。目前,我正在从 MySQL 数据库中获取数据、显示并更新它。我能够将值零保存到数据库,并且从数据库返回的信息是正确的,因为我可以在将值直接回显到页面时查看它,并且零以外的任何值都可以正确显示。有谁知道如何启用以锂形式呈现零值,或者可能在以锂形式呈现之前禁用值检查?

我在<?=$this->form->field...绑定到对象的表单中使用锂提供的默认值<?=$this->form->create($object); ?>

4

1 回答 1

2

哈。我以前从未注意到这一点。好像应该修好了。

同时,您可以在您的应用程序中编辑lithium\template\helper\Form或创建(如果您将其从默认值更改为应用程序的命名空间,则app\extensions\helper\Form替换为应用程序的命名空间)。appapp

需要改变的方法是_defaults()。以下是将显示零值的内容:

protected function _defaults($method, $name, $options) {
    $methodConfig = isset($this->_config[$method]) ? $this->_config[$method] : array();
    $options += $methodConfig + $this->_config['base'];
    $options = $this->_generators($method, $name, $options);

    $hasValue = (
        (!isset($options['value']) || $options['value'] === null) &&
        $name && $value = $this->binding($name)->data
    );
    $isZero = (isset($value) && ($value === 0 || $value === "0"));
    if ($hasValue || $isZero) {
        $options['value'] = $value;
    }
    if (isset($options['value']) && !$isZero) {
        $isZero = ($options['value'] === 0 || $options['value'] === "0");
    }
    if (isset($options['default']) && empty($options['value']) && !$isZero) {
        $options['value'] = $options['default'];
    }
    unset($options['default']);

    $generator = $this->_config['attributes']['name'];
    $name = $generator($method, $name, $options);

    $tplKey = isset($options['template']) ? $options['template'] : $method;
    $template = isset($this->_templateMap[$tplKey]) ? $this->_templateMap[$tplKey] : $tplKey;
    return array($name, $options, $template);
}

当前版本有一些条件失败,实际为零整数或字符串"0"

于 2012-05-30T21:57:44.607 回答