Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

howto display validation errors in freemarker template

all approches i do to display my validation errors in a freemarker template fail. i use spring mvc version 3.

My form looks like that

<@layout.basic>
<@spring.bind "user" />
<#if spring.status.error>
    <div class="errors">
        There were problems with the data you entered:
        <ul>
            <#list spring.status.errorMessages as error>
                <li>${error}</li>
            </#list>
        </ul>
    </div>
<#else>
    <div class="errors">
        There are no errors.
    </div>
</#if>
<form action="" method="POST">
    <dl>
        <dt>Login:</dt>
            <dd><@spring.formInput  "user.name" />
            <dd><@spring.showErrors "<br>" />
        <dt>E-Mail:</dt>
            <dd><@spring.formInput "user.email" />
            <dd><@spring.showErrors "<br>" />
        <dt>Password:</dt>
            <dd><@spring.formPasswordInput "user.passwort" />
            <dd><@spring.showErrors "<br>" />
        <dt>Password verification:</dt>
            <dd><input type="password" name="passVerification"/>
            <dd><@spring.showErrors "<br>" />
        <dt>Should the User have administrator rights?</dt>
            <dd><input type="checkbox" name="isAdmin"/>
            <dd><@spring.showErrors "<br>" />
        <br>
            <dd><input type="submit" value="Create" />
    </dl>
</form>

My basic layout looks like that

<#macro basic> 
<#-- needed for query spring security status -->
<#assign security=JspTaglibs["http://www.springframework.org/security/tags"] />
<!DOCTYPE HTML>
<html>
    <head>
        <title>Testform</title>
    </head>
    <body>
        <div id=header>
            <@security.authorize ifAnyGranted="ROLE_ADMIN">
                <a href='<@spring.url "/user/add" />'>Add user | </a>
                <a href='<@spring.url "/user/manage" />'>Manage users | </a>
            </@security.authorize>    
            <@security.authorize ifAnyGranted="ROLE_USER">
                <a href='<@spring.url "/job/add" />'>Add job | </a>
                <a href='<@spring.url "/job/show" />'>Show jobs | </a>
            </@security.authorize>
        </div>
        <div id=errors>
        </div>
        <div id=content>
            <#nested>
        </div>
        <div id=footer>
            <@security.authorize ifAnyGranted="ROLE_USER">
                <a href='<@spring.url "/j_spring_security_logout" />'>Logout</a>
            </@security.authorize>
        </div>
    </body>
</html>
</#macro>

I defined the spring.ftl in my servlet config

<property name="freemarkerSettings">
    <props>
        <prop key="auto_import">layout.ftl as layout, spring.ftl as spring</prop>
    </props>
</property>

And my Controller looks like this

@RequestMapping( value = "/add", method = RequestMethod.POST ) 
public String addUser(
        @RequestParam(value="isAdmin",defaultValue="false") Boolean isAdmin,
        @RequestParam(value="passVerification" ) String passVerification,
        @ModelAttribute("user") C_UserDAO newUser
) {
    final BindException errors = new BindException( newUser, "user" );
    m_userValidator.validate( newUser, errors );
    ...
    if( !newUser.getPassword().equals( passVerification ) && !newUser.getPassword().equals( "" ) ) {
        errors.rejectValue( "password", "user.password.missmatch", "The passwords aren't equal, try again" );
    }
    if( errors.hasErrors() ) {
        return "addUserForm";
    }
    ...
    return "redirect:thanks.html";
}

The validation works like a charm, but when an error occures, the view doesn't change and no error is shown. I've read the documentation over and over again but i can't find a clou how to solve the problem. What's my error?

like image 922
lofthouses Avatar asked Dec 12 '22 19:12

lofthouses


1 Answers

I'm not familiar with FreeMarker, but I see your BindingResult is not associated with the model. You need to add it to the signature of your method right after the corresponding model attribute:

@RequestMapping( value = "/add", method = RequestMethod.POST )  
public String addUser( 
        @RequestParam(value="isAdmin",defaultValue="false") Boolean isAdmin, 
        @RequestParam(value="passVerification" ) String passVerification, 
        @ModelAttribute("user") C_UserDAO newUser,
        BindingResult errors
) { 
    ...
}
like image 65
axtavt Avatar answered Mar 05 '23 03:03

axtavt