Symfony:将日期时间表单字段设置为只读 [英] Symfony: Make a datetime form field really read-only
问题描述
我将日期时间表单字段标记为read_only.由于此接缝无效,因此我否决了默认的树枝模板.效果很好.
I marked a datetime form field as read_only. Since this seams to have no effect I overruled the default twig template. This works fine.
{% 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 %}
这是我正在使用的表单类型:
This is the form type I'm using:
/**
* @ORM\Entity
*/
abstract class BusinessClass
{
/**
* @ORM\Column(type="datetime")
* @var \DateTime
*/
private $created;
/**
* @return \DateTime
*/
public function getCreated()
{
return $this->created;
}
}
这是将datetime字段添加到表单构建器的方式:
And this is how the datetime field is added to the form builder:
$formBuilder->add('created', 'datetime', array('read_only' => true));
但是,symfony仍然尝试将未更改的值设置回我的表单类型.由于它是只读的,因此我仅实现了get方法,而没有实现set方法.这就是为什么我收到此错误消息的原因:
However, symfony still tries to set the unchanged value back to my form type. Since it's read-only I only implemented a get method but not set method. That's why I get this error message:
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方法.还值得一提的是,对于文本字段,同样的事情也可以正常工作.
Since this is part of my business logic I don't want to implement a setCreate method. It's also woth mentioning, that the very same thing works fine for text fields.
如果设置了read_only
选项,关于如何阻止symfony设置值的任何想法?
Any ideas about how I can stop symfony from setting the value if the read_only
option is set?
推荐答案
简短的答案是:使用disabled
而不是read_only
.
The short answer is: Use disabled
instead of read_only
.
在文档中找到相关部分我认为read_only
的datetime呈现不正确.仅设置read_only
不变.根据文档,应将其改写为只读HTML.
After finding the relevant part in the docs I think that the rendering of datetime for read_only
is not correct. Only setting read_only
is changing nothing. According to the docs it should be rednered as read only HTML.
这篇关于Symfony:将日期时间表单字段设置为只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!