Error on form submit

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Error on form submit

bigal.nz
Hi,

I have a form that on Submit throws error : Validation failed for object='episode'. Error count: 1

I have a linked hashmap to generate a dropdown like so:


The code to generate this dropdown with optgroup dividers is:

                    <select size="3" name="planLead" class="selectpicker show-tick" id="planLead" data-width="100%"
                            title="Choose" th:field="*{plan.planLead}">
                        <optgroup th:each="pairUsersOrganisations : ${organisations}"
                                  th:label="${pairUsersOrganisations.key}">
                            <option th:each="user : ${pairUsersOrganisations.value}"
                                    th:value="${user.userId}"
                                    th:selected="${user.userId} == *{plan.planLead}"
                                    th:text="${user.firstname} +' ' +${user.surname}">
                            </option>
                        </optgroup>
                    </select>
                </div>

The id of the selected user should go into :

episode.plan.planLead

Where plan lead is in entity plan as :
   
    @OneToOne
    private User planLead;

and User has :

    public User() {}

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "user_id")
    private int id;

I am struggling to work out why I am getting the validation error.

Al


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error on form submit

bigal.nz
Also, if I update the plan table on column for planLead user id to hold an id, then try to load the plan page I get a different error:

Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringOptionFieldAttrProcessor' (newplan:265)

265: th:text="${user.firstname} +' ' +${user.surname}">

A child to episode thats getting loaded into the view is plan which has this structure:

Also the linked hashmap for users by organisation:





Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error on form submit

danielfernandez
Administrator
At first sight this seems an issue at your Spring MVC code, not Thymeleaf.

Your Plan.planLead property is of type User, but your form seems to be sending, for the "planLead" form field, a value of type int (th:value="${user.userId}").

This is fine and it's the correct way to do it, but it needs an additional piece of software: a Formatter registered at your Spring MVC side that is able to transform that "int userId" coming from the browser into a full "User" object that can be set into your "Plan.planLead" property at the form-backing bean.

Do you already have such Formatter in place? If not, you can have a look at a couple of examples of that at the STSM example application, like: https://github.com/thymeleaf/thymeleafexamples-stsm/blob/f368728a5e024c1604f862c307e425b219ace554/src/main/java/thymeleafexamples/stsm/web/conversion/VarietyFormatter.java

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error on form submit

bigal.nz
That kind of makes sense - never done one before, but I will have a go.

I am sure somewhere else I have done this without the formatters .... but anyhow I will give it a go and update post.

Thanks for the reply.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error on form submit

bigal.nz
In reply to this post by danielfernandez
Do I have to cal that class for POST and GET?

GET loads if I dont have a user associated to a plan.

If I do have a user it wont load the page (even though there is a user there)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error on form submit

danielfernandez
Administrator
You don't call the Formatter class at all, Spring MVC does. You simply create and register it (usually just adding a @Component annotation is enough for that).

Then Spring MVC will detect that you are trying to set an int into a User but note that there is a Formatter that is able to transform between the two, and it will cal it.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error on form submit

bigal.nz
Do you have a link to it being registered in your github example?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error on form submit

danielfernandez
Administrator
https://github.com/thymeleaf/thymeleafexamples-stsm/blob/f368728a5e024c1604f862c307e425b219ace554/src/main/java/thymeleafexamples/stsm/web/SpringWebConfig.java#L91-L96

But that is not a Spring Boot application, so explicit registering is needed. If yours is a Spring Boot app, all you need to do is to simply declare them as beans in your config (like https://github.com/thymeleaf/thymeleafsandbox-stsm-mvc/blob/d5e48701739016e9b3168cff65dbcbeef295a943/src/main/java/thymeleafsandbox/stsm/StsmMvcWebConfig.java) or maybe just add a @Component annotation to their classes.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error on form submit

danielfernandez
Administrator
Anyway, all of this is Spring MVC / Spring Boot, not Thymeleaf. So you might be better off referring to Spring's documentation on the topic.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error on form submit

bigal.nz
I got it going. Thanks
Loading...