1

In my SpringMVC form, I was facing the problem of binding a Disabled Text Box to a Model object. The issue is that, even if initially populated, after submitting the form the Disabled box's contents are lost on the next page refresh, because Disabled fields aren't submitted.

However, I solved this by just adding a Hidden field that binds to the same property. Now my Disabled text box retains its contents even after submission. Is this the correct approach, if I need to always show the property value in my Disabled box?

<form:input path="signatureBlock.signature" disabled="true"/>
<!--  Hidden field to submit Signature with form, binds to the same Model property -->
<form:hidden path="signatureBlock.signature" />
gene b.
  • 10,512
  • 21
  • 115
  • 227
  • It will create one more element with the same id. And it could break some logic. But in general it works. – MrBlack Feb 18 '22 at 11:57

1 Answers1

3

I would prefer to suggest you this approach

<form:input path="signatureBlock.signature" readonly="true"/>

Here's difference definition between disabled and readonly html attribute

but the real goal is not to persist the value into the db. It really doesnt matter(in the context of modifing field value) if you set field value as disabled or read-only because if user removes these attributes from the post form(in html) the field value will be bound to the form anyway. The solution is not to persist the 'disabled' values in the method wich converts form to the db object or set the proper init binder with allowed fields.

Community
  • 1
  • 1
Oskar Dajnowicz
  • 1,700
  • 12
  • 16