get the selected objects from checkboxes

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

get the selected objects from checkboxes

julia
Hello,

       I have customer update page. There are checkboxes represent the customerType objects. These checkboxes are generated according list.
When I choose the customer to update, the some of related checkboxes should already selected in update page.
When I select the other checkboxes and save, My controller cannot retrieve the selected value object from request form.
           I use 'th:field' to get the list of selected object. Here is my coding...
           <label th:each="type : ${customerTypes}">
                <input type="checkbox" th:field="${selectedValues}"
                         th:value="${type}"
                         th:text="${type.name}">
                </input>
               </label>

                                       
        This is controller method.
                                       
        public String processUpdateAccount(HttpServletRequest request, Model model,
                        @ModelAttribute("updateAccountForm") UpdateAccountForm form,
                        BindingResult result, RedirectAttributes redirectAttributes)
                        throws ServiceException {
                        System.out.println(form.getCustomerTypes.size() + " size");
                         
                }

        It print 0 size.

I have CustomerType list in UpdateAccountForm class.
I would like to know that
Is is possible to parse object list by using 'th:field'? How should I do?

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: get the selected objects from checkboxes

Zemi
Administrator
Hello,

th:field is intended to be used with *{...} operator.

On the other hand, different checkboxes need different form names. If you want to bind the chechboxes to a List, their names should be
    customerTypes[0], customerTypes[1], customerTypes[2]...
(provided you have a field called customerTypes in your command bean).

By the way, the HTML element <label> has a different syntax, see for example
    http://www.w3schools.com/tags/tag_label.asp
(I agree that your syntax is better than standard's).

Reply | Threaded
Open this post in threaded view
|

Re: get the selected objects from checkboxes

julia
Thank u for reply.

 yes I used with *{...} operator. Customertype is an object type. I want to parse that object into the controller.

I'd like to know can I parse the object type with th:field

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: get the selected objects from checkboxes

Priya
u can only use standard java objects, for customized ones, make a customized class
Reply | Threaded
Open this post in threaded view
|

Re: get the selected objects from checkboxes

marcelo
Hi,

Could you give more details regarding binding to custom objects. I'm having trouble to bind a list of custom objects to a form. Would appreciate for any tips. My html looks like this:

<form id="addFromGalleryForm" name="addFromGalleryForm" th:object="${addFromGalleryForm}" th:action="@{/videos/addFromGallery}" method="post">
<div>
        <div th:each="item, stat : ${userVideos}">
                <fieldset>
                        <input type="hidden" th:field="*{videos[__${stat.index}__].id}" th:value="${item.id}" />
                        <input type="hidden" th:field="*{videos[__${stat.index}__].filename}" th:value="${item.filename}" />
                        <div>
                                <input type="text" class="text" th:field="*{videos[__${stat.index}__].videoName}" th:value="${item.videoName}" />
                        </div>
                        <div class="popupInputRow checkboxPosition">
                                <p class="customForm"><input type="checkbox" id="video1" /><label></label></p>
                        </div>
                </fieldset>
        </div>
</div>

and i have 'List<UserFile> videos' field in my page bean.

Everytime i submit the form i get bad request http error