Symfony的:让真正只读日期时间表单字段(Symfony: Make a datetime fo

2019-09-26 10:35发布

我为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选项设置?

Answer 1:

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

找到后, 在文档中相关部分我认为日期时间为呈现read_only是不正确的。 只有设置read_only是变着法子。 根据该文档应该rednered为只读HTML。



Answer 2:

不幸的是,如果一个映射的字段设置为禁用它,然后不持久。 它的值变为零,这可能不是什么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 %}

其中,家庭是窗体名称,dateAdded是映射的字段名。



文章来源: Symfony: Make a datetime form field really read-only