我为READ_ONLY打上日期时间表单字段。 由于该接缝有没有效果我否决默认树枝模板。 这工作得很好。
{% 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方法,但不能设置方法。 这就是为什么我收到此错误信息:
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方法。 这也是woth提的是,同样的事情工作正常的文本字段。
我如何能够从如果设定值停止symfony的任何想法read_only
选项设置?