Forms inside th:each issue

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

Forms inside th:each issue

                                                                                                                       
                                       
                                        <th:block th:switch="${user.active}">
                                                                                                                                </th:block>
                                                                                                                                                                                                <form th:action="@{/admin/user/update}" method="post" th:object="${user}">

                                        <input type="hidden" th:field="*{id}" value="*{id}" />
                                        </form>
                                                                    </th:block>
......
Note: i would like to avoid going with get method and to insert new User in my controller (or maybe its the only way?)

Any help would be much appreciated! Thanks!
vibetribe93
ok so I have an issue with for loop and forms inside of it. I am having multiple users which I get through th:for each which I am trying to update but i keep getting

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute

It doesn't make any since to me since I am trying to reference the list object itself for each form...

the code:

<th:block th:each="user : ${users}">
                                   
                                            <p th:text="${user.name}"></p>
                                            <p th:text="${user.lastName}"> </p>
                                       
Secured<th:block th:each="role : ${user.roles}">
                                                <p th:text="${role.role}"></p>
                                            </th:block>
YesNocreate
Reply | Threaded
Open this post in threaded view
|

Re: Forms inside th:each issue

yvanavermaet
I don't think it's possible what you're trying to do. According to the documentation: http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html

Values for th:object attributes in form tags must be variable expressions (${...}) specifying only the name of a model attribute, without property navigation. This means that an expression like ${seedStarter} is valid, but ${seedStarter.data} would not be.

In your case, user is an element from a list. I think that's the reason why your setup doesn't work.