dsfdf854456 2015-12-08 21:04
浏览 58
已采纳

symfony隐藏字段,但对它们进行验证

I have a strange issue with one of my entities.

my Vehicle entity has the following db table fields:

  • orders (PK)
  • licenseplate (PK)
  • make
  • model
  • variant

On the form the only field visible is the licenseplate.

After the field there is a button where the visitor fetches the make/model/variant data from a ajax call to a AjaxController that adds the data to the db. (there is a $session 'orderId' that sets the 'orders' field).

I need to be absolutely certain that the visitor has fetched the make/model/variant and that the visitor cannot change the make/model/variant data.

How can I check on form submit that the vehicle table is filled out? Can I do a Doctrine check and set the form to 'invalid'?

Thank you for your time.

Regards, Lars Hansen

  • 写回答

1条回答 默认 最新

  • drj26159 2015-12-09 19:02
    关注

    One approach would be:

    1. Remove make, model and variant from your form. If you don't want the user potentially changing them and you're saving them elsewhere, there's no point having them.
    2. Add a hidden, non-mapped field, something like 'dataFetched' to the form.
    3. When successfully fetching/saving the car data via ajax, set this field checked/true
    4. Via javascript, don't allow form submission until the dataFetched field is checked.
    5. In a PRE_SUBMIT listener, check that dataFetched is true and if not, invalidate the form (because a JS check is not sufficient on its own). See CsrfValidationListener in the symfony standard package for a listener example. (I'm using 2.3 but I assume it's still there in newer versions).
    6. Optionally If you wanted to keep make/model/variant in the form. In the listener you could also check that make/model/variant corresponds to what you would expect it to (in case the user changes it) & invalidate the form at that point.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低