0

I have a list of custom objects that I use as the commandName object in my form, and usually each object contains a String label, String type (INTEGER or BOOLEAN), and a value. For the INTEGER types, the form displays the <form:input and binds just fine. However, with BOOLEAN types, I'd like to be able to use the values strings of 'true' and 'false' and bind them to a checkbox. This would be easy is they were Boolean types, but they are Strings, and the normal <form:checkbox code, isn't working. I tried using hidden too, but it isn't binding correctly. Is there an easy way to do this? When I submit the form again, the checkbox values need to be a string again, i.e. (checked="true", unchecked="false").

1 Answer 1

1

Checkboxes don't really have multiple values - either it has the value, indicating it was checked, or it doesn't. In your case, I would bind the value you want to pass back and forth to a hidden form field, and use client-side JavaScript to set the value of the hidden form field based on whether the user clicks the checkbox or not. Additionally, on page load, Spring will set the value of the bound hidden field, and at that point you can use JavaScript to check the checkbox, or not, based on the bound value in the hidden field.

Spring Form:

<form:hidden path="mypassedfield" />

jQuery:

if($(".mycheckbox").prop("checked", true)){
   $(".mypassedfield").val($(".mycheckbox").val());
}
1
  • sorry but this is horrible. Isn't there a better way to do it, like with Converters, or something like that? Can't a Converter be written to convert String->Boolean to permit auto checkbox binding???
    – gene b.
    Jan 16, 2017 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.