How to split form into fragments?

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

How to split form into fragments?

popov.spb
In my spring-mvc project two JPA entities have Organization property with different names:
@Entity
public class Video extends AbstractIdentifiable implements Serializable {

    @ManyToOne
    @JsonIgnore
    @JoinColumn(nullable = false)
    private Organization customerOrganization;

    public Organization getCustomerOrganization() {
        return customerOrganization;
    }

    public void setCustomerOrganization(Organization customerOrganization) {
        this.customerOrganization = customerOrganization;
    }

}

@Entity
public class Location extends AbstractIdentifiable implements Serializable {

    @ManyToOne
    @JoinColumn(nullable = false)
    @JsonIgnore
    private Organization organization;

    public Organization getOrganization() {
        return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

}
Superfluous is removed. I have a form in the thymeleaf template for Location, give the part responsible for editing Organization:
<div class="col-md-4 mb-3">
    <label for="organization" th:text="#{organization}"/>
    <select class="form-control" id="organization" name="organization" th:attrappend="class=${#fields.hasErrors('organization') ? ' is-invalid' : ''}" th:field="*{organization}">
        <option th:each="organization: ${organizations}" th:value="${organization.id}" th:text="${organization.title}"/>
    </select>
    <div class="invalid-feedback" th:if="${#fields.hasErrors('organization')}">
        <p th:each="error: ${#fields.errors('organization')}" th:text="${error}"/>
    </div>
</div>
I would like to take out it in a fragment to use for all entities where there is this property for not to copy and paste a code, considering that the entity can have a different name of property.
The block given above is replaced on:
<div th:replace="~{forms :: organization('organization', *{organization})}"/>
Fragment:
<div th:fragment="organization(fieldName, organization)" class="col-md-4 mb-3">
    <label th:for="${fieldName}" th:text="#{organization}"/>
    <select th:id="${fieldName}" th:name="${fieldName}"
            th:attrappend="class=${#fields.hasErrors(fieldName) ? ' is-invalid' : ''}"
            th:field="${organization}" class="form-control">
        <option th:each="organization: ${organizations}" th:object="${organization}"
                th:value="*{id}"
                th:text="*{title}"/>
    </select>
    <div class="invalid-feedback" th:if="${#fields.hasErrors(${fieldName})}">
        <p th:each="error: ${#fields.errors(${fieldName})}" th:text="${error}"/>
    </div>
</div>
As a result I receive error:
<raw>java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'organization' available as request attribute
Problem with th:field because it in a fragment he doesn't see a form as I have understood. How fix this? Maybe for repeated use of the themeleaf templates in forms there is other way, besides fragments?
Excuse me for bad English.
Reply | Threaded
Open this post in threaded view
|

Re: How to split form into fragments?

Reiju
Looks like you need * instead of $ in th:field="${organization}" in fragment. Or you can additionally pass independent "organization" entity.