简体   繁体   中英

Thymeleaf Validation

I am learning thymeleaf validation section and i got error like

Exception evaluating SpringEL expression: "#fields.hasErrors('jobtitle')" (authentication/contactus:19)

My form has following field

        <div class="form50">
            <label for="contact.emailAddress"><span th:text="#{contact.email}">Email</span></label>
             <span class="error" th:if="${#fields.hasErrors('email')}" th:errors="email"></span>
            <input type="email" th:field="*{email}" class="field50" th:classappend="${#fields.hasErrors('email')}? 'fieldError'" />
        </div>

        <div class="form50">
            <label for="customer.firstName"><span th:text="#{contact.jobtitle}">Job Title</span></label>
            <span class="error" th:if="${#fields.hasErrors('jobtitle')}" th:errors="*{jobtitle}"></span>
            <input type="text" th:field="*{jobtitle}" class="field50" th:classappend="${#fields.hasErrors('name')}? 'fieldError'" />
        </div>

        <div class="login_register">
            <input class="register_button big red" type="submit" th:value="#{contact.contact}"/>
        </div>

    </blc:form>

when i remove jobtitle div its working fine

myvalidator class looks like following

public void validate(Object obj, Errors errors, boolean useEmailForUsername) {
        ContactCustomerForm form = (ContactCustomerForm) obj;
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "name.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "emial.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "jobtitle", "jobtitle.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "country", "country.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "phone", "phone.required");
}

What is the issue i cant able to identify!!!!!!Please Help

Problem is following line

<span class="error" th:if="${#fields.hasErrors('email')}" th:errors="email"></span>

it has to be

th:errors="*{email}"

same as this line in your code bellow

<span class="error" th:if="${#fields.hasErrors('jobtitle')}" th:errors="*{jobtitle}"></span>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM