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 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探