douou8954 2013-11-13 21:20
浏览 33
已采纳

如何自定义编辑表单

I already had a post that lead to this question but now I isolated one problem and so its better to make a new clean post :

Symfony 2.1.3 with jordillonch/CrudGeneratorBundle

I used this CrudGenerator on an entity in which I have 2 timestampable fields that need to be updated automatically : I installed Gedmo for the timestampable funtionnality.

The fields are :

cree_le (in english created_at) : @Gedmo\Timestampable(on="create")

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

The problem is that I want to customize the form to show only the fields to update by the user. When I use the not customized edit.html.twig using {{ form_widget(edit_form) }} the update is working but the values of updated_at are not changed because the old value is submitted with the form. I tried several things to customize the form but I did not yet find the solution.

This version of customized edit.html.twig is not working because the submitted form is not valid :

<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>

Sure I'm doing something wrong but who could tell me how to go on ?

  • 写回答

1条回答 默认 最新

  • dsbo44836129 2013-11-14 00:07
    关注

    Remove cree_le and modifiele from your Form Class and dont render them on your template. Then let Timestampable do his job! when you create something the cree_le value will be automatic set. When you change something the modifiele value will be automatic set too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算