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 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重