我将日期时间表单字段标记为只读。由于这个接缝没有效果,我否决了默认的树枝模板。这工作正常。
{% 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
?