【问题标题】:How to customize Edit-Form如何自定义 Edit-Form
【发布时间】:2013-11-13 21:20:39
【问题描述】:

我已经有一个帖子导致了这个问题,但现在我孤立了一个问题,所以最好写一个新的干净的帖子:

带有 jordillonch/CrudGeneratorBundle 的 Symfony 2.1.3

我在一个实体上使用了这个 CrudGenerator,其中我有 2 个需要自动更新的时间戳字段: 我安装了 Gedmo 以实现可时间戳功能。

这些字段是:

cree_le(英文 created_at):@Gedmo\Timestampable(on="create")

and modifiele (in English updated_at) - @Gedmo\Timestampable(on="update")

问题是我想自定义表单以仅显示用户要更新的字段。 当我使用 {{ form_widget(edit_form) }} 使用未自定义的 edit.html.twig 时,更新正在工作,但 updated_at 的值没有更改,因为旧值与表单一起提交。 我尝试了几种方法来自定义表单,但我还没有找到解决方案。

这个版本的自定义edit.html.twig不工作,因为提交的表单无效:

<form class="well" action="{{ path('employee_update', { 'id': entity.id }) }}" method="post" {{ form_enctype(edit_form) }}>
    <input type="hidden" name="_method" value="PUT" />
    <div>
        {{ form_label(edit_form.nom) }}
        {{ form_errors(edit_form.nom) }}
        {{ form_widget(edit_form.nom) }}
    </div>
    <div>
        {{ form_label(edit_form.email) }}
        {{ form_errors(edit_form.email) }}
        {{ form_widget(edit_form.email) }}
    </div>
    <div>
        {{ form_label(edit_form.telephone, 'Téléphone') }}
        {{ form_errors(edit_form.telephone) }}
        {{ form_widget(edit_form.telephone) }}
    </div>
    <div>
        {{ form_label(edit_form.actif) }}
        {{ form_errors(edit_form.actif) }}
        {{ form_widget(edit_form.actif) }}
    </div>
    <input type="hidden" id="too_employeebundle_employee_cree_le" name="too_employeebundle_employee[cree_le]" value="{{ entity.creele|date('Y-m-d H:i:s') }}">
    <input type="hidden" id="too_employeebundle_employee_modifie_le" name="too_employeebundle_employee[modifie_le]" value="{{ entity.modifiele|date('Y-m-d H:i:s') }}">
    {{ form_widget(edit_form._token) }}
    <p>
        <button type="submit" class="btn btn-success">{{ 'views.edit.editbutton'|trans({}, 'JordiLlonchCrudGeneratorBundle') }}</button>
    </p>
</form>

当然我做错了什么,但谁能告诉我该怎么做?

【问题讨论】:

    标签: php forms symfony


    【解决方案1】:

    从你的表单类中移除 cree_le 和 modifiele 并且不要在你的模板上渲染它们。 然后让 Timestampable 完成他的工作!当您创建某些东西时,会自动设置 cree_le 值。当您更改某些内容时,修改值也会自动设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-26
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多