Form inside th.each

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

Form inside th.each

Zemi
Administrator
Hi,

I am iterating over the values of a List<Product> name "products".

                        <li th:each="product : ${products}">
                                < span th:text="${product.name}">Microwave< /span>.
                        </li>

Now I want to add a "Delete" button and I can't figure out how to reference a form th:object inside a th:each.
The code

                        <li th:each="product : ${products}">
                                < span th:text="${product.name}">Microwave< /span>.
                                <form action="productDelete" method="post" th:object="${product}">
                                        <input type="hidden" name="productId" th:field="*{id}" />
                                        <input type="submit" value="Borrar" />
                                </form>
                        </li>
throws the error "Neither BindingResult nor plain target object for bean name 'product' available as request attribute"

Which is the correct syntax for this case?
Reply | Threaded
Open this post in threaded view
|

Re: Form inside th.each

danielfernandez
Administrator
Hi,

If all you want is to add a "productId" parameter to your form URL, there's no need for you to use "product" as a form-backing bean or use the th:field attribute.

Maybe something like this will just work for you:

<li th:each="product : ${products}">
    < span th:text="${product.name}">Microwave< /span>.
    <form action="productDelete" method="post">
        <input type="hidden" name="productId" th:value="${product.id}" />
        <input type="submit" value="Borrar" />
    </form>
</li>

Regards,
Daniel.

Reply | Threaded
Open this post in threaded view
|

Re: Form inside th.each

Zemi
Administrator
Ok, that worked!

I was trying a bit too complicated solution...

Thanks a lot!