3

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?

Pere Villega
  • 16,429
  • 5
  • 63
  • 100
Dean Hiller
  • 19,235
  • 25
  • 129
  • 212

1 Answers1

6

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
tmbrggmn
  • 8,680
  • 10
  • 35
  • 44
  • 1
    Another alternative is to have a second hidden field which contains the data. Also, you may find that play still has it in the params object request but doesn't bind it. – grahamrb Feb 08 '12 at 09:17
  • @grahamrb Oh it still keeps them in the request? Didn't know that, good call. – tmbrggmn Feb 08 '12 at 09:23
  • @tmbrggmn the hidden field option is the best alternative, more resilient – Pere Villega Feb 08 '12 at 14:35
  • @PereVillega I agree, on the condition that the OP doesn't actually want to show this field. I suspect that he may still want to show the input field in the form, but rather not allow any changes to it. – tmbrggmn Feb 08 '12 at 14:42
  • 1
    @tmbrggmn then he can display it as a text label, neater than a disabled input :) – Pere Villega Feb 08 '12 at 14:43