1

我将日期时间表单字段标记为只读。由于这个接缝没有效果,我否决了默认的树枝模板。这工作正常。

{% block datetime_widget %}
    {% if read_only %}
        {{ value.date.year }}-{{ value.date.month }}-{{ value.date.day }} {{ value.time.hour }}:{{ value.time.minute }}
    {% else %}
        {{ parent() }}
    {% endif %}
{% endblock %}

这是我正在使用的表单类型:

/**
 * @ORM\Entity
 */
abstract class BusinessClass
{

    /**
     * @ORM\Column(type="datetime")
     * @var \DateTime
     */
    private $created;

    /**
     * @return \DateTime
     */
    public function getCreated()
    {
        return $this->created;
    }

}

这就是将日期时间字段添加到表单构建器的方式:

$formBuilder->add('created', 'datetime', array('read_only' => true));

但是,symfony 仍然尝试将未更改的值设置回我的表单类型。由于它是只读的,我只实现了一个 get 方法,但没有实现 set 方法。这就是我收到此错误消息的原因:

InvalidPropertyException: Neither element "created" nor method "setCreated()" exists in class "Cinergy\ShopBundle\Tests\Functional\TestBundle\Entity\RecurringProduct"

    in /Users/ernst/Source/php/cinergy/shop/vendor/symfony/symfony/src/Symfony/Component/Form/Util/PropertyPath.php line 552
    at PropertyPath->writeProperty(object(RecurringProduct), 'created', null, false, null) in /Users/ernst/Source/php/cinergy/shop/vendor/symfony/symfony/src/Symfony/Component/Form/Util/PropertyPath.php line 318
    at PropertyPath->setValue(object(RecurringProduct), null) in /Users/ernst/Source/php/cinergy/shop/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php line 74
    at PropertyPathMapper->mapFormsToData(array('created' => object(Form), 'duration' => object(Form), 'id' => object(Form), 'name' => object(Form), 'price' => object(Form), 'sku' => object(Form), 'updated' => object(Form), '__entity' => object(Form), '__id' => object(Form)), object(RecurringProduct)) in /Users/ernst/Source/php/cinergy/shop/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 569
    at Form->bind(object(Request)) in /Users/ernst/Source/php/cinergy/shop/src/Cinergy/DaylightBundle/Controller/BrowserController.php line 123
    at BrowserController->updateAction(object(Request))
    at call_user_func_array(array(object(BrowserController), 'updateAction'), array(object(Request))) in /Users/ernst/Source/php/cinergy/shop/src/Cinergy/CommonBundle/EventListener/TransactionWrapper.php line 34
    at TransactionWrapper->wrappedExecution(object(Request))
    at call_user_func_array(array(object(TransactionWrapper), 'wrappedExecution'), array(object(Request))) in /Users/ernst/Source/php/cinergy/shop/app/bootstrap.php.cache line 1426
    at HttpKernel->handleRaw(object(Request), '1') in /Users/ernst/Source/php/cinergy/shop/app/bootstrap.php.cache line 1390
    at HttpKernel->handle(object(Request), '1', true) in /Users/ernst/Source/php/cinergy/shop/app/bootstrap.php.cache line 1566
    at HttpKernel->handle(object(Request), '1', true) in /Users/ernst/Source/php/cinergy/shop/app/bootstrap.php.cache line 617
    at Kernel->handle(object(Request)) in /Users/ernst/Source/php/cinergy/shop/web/app_dev.php line 28

由于这是我的业务逻辑的一部分,我不想实现 setCreate 方法。还值得一提的是,同样的事情也适用于文本字段。

如果设置了选项,关于如何阻止 symfony 设置值的任何想法read_only

4

2 回答 2

3

简短的回答是:使用disabled而不是read_only.

在文档中找到相关部分后,我认为日期时间的呈现read_only不正确。只有设置read_only没有改变。根据文档,它应该被重新定义为只读 HTML。

于 2013-03-18T08:11:46.483 回答
-1

不幸的是,如果将映射字段设置为禁用,则不会保留它。它的值变为空,这可能不是 OP 正在寻找的。另一种解决方案是在您的模板中执行以下操作:

{% if household.dateAdded is not null %}{{ household.dateAdded|date('m/d/Y') }}
<input type="hidden" name="household[dateAdded]" value="{{ household.dateAdded|date('m/d/Y') }}">
{% endif %}

其中family 是表单名称,dateAdded 是映射的字段名称。

于 2013-10-09T20:07:24.763 回答