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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分