playframework, input disabled breaks play from passing the value?

杀马特。学长 韩版系。学妹 提交于 2020-01-13 16:28:19

问题


I have an input field that is filled in from a previous form(so the input is set to disabled on the second page) and we receive null for the value then. This works:

<input type="text" class="boxtpl" name="${field.name}" value="${user?.email}">

but this doesn't:

<input type="text" class="boxtpl" name="${field.name}" value="${user?.email}" disabled="disabled">

Is there a reason why this seems to break the framework?


回答1:


Disabled controls shouldn't actually be submitted with the form, so what you're seeing is in fact normal behaviour. According to the HTML form specification:

When set, the disabled attribute has the following effects on an element:

  • Disabled controls do not receive focus.
  • Disabled controls are skipped in tabbing navigation.
  • Disabled controls cannot be successful.

The definition of successful can be found in the same document. It's a bit nonsensical to suggest that Play is broken because of this.

If you want to have a form field that user cannot edit while it should still be sent along when the form is submitted, you can use the read-only attribute, or use JavaScript to disallow user input.

Update: as pointed out in the comments, the following points may also offer a solution:

  • It's possible that Play still keeps the disabled control's form values in the request object, and just doesn't bind them (so you could retrieve them from the request if needed)
  • Use a hidden field to keep the form value in case you still want to submit the value, but do not want the user(s) to see the control


来源:https://stackoverflow.com/questions/9187516/playframework-input-disabled-breaks-play-from-passing-the-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!