Search code examples
javajpatapestrybean-validation

Complex use case about JSR-303, Tapestry, and JPA entity inheritance


I have a JPA entity called ParentAccount that extends an abstract Account entity (see JPA inheritance). I have place the JSR-303 validation constraints in the Account entity. Now I have the following Tapestry class and templates and JSR-303 validation does not seem to work:

Tapestry class:

 public class Inscription {        
    @Property
    //this is not validated...
    private ParentAccount parentAccount;

    @Property
    @Validate("required")
    private String accountPasswordConfirmation;    

    @InjectComponent
    private Form registrationForm;

    @OnEvent(EventConstants.PREPARE)
    void prepareAccount(){
        parentAccount = new ParentAccount();
    }

    @OnEvent(value= EventConstants.VALIDATE)
    void validateRegistrationForm() {
        if(registrationForm.isValid()) {
            if(accountPasswordConfirmation.equals(parentAccount.getAccountPassword())) {
                System.out.println("ok for insert");
            }
        }
    }
}

Tapestry page:

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
<head>
    <title>Hello World Page</title>
</head>
<body>
<form t:type="form" t:id="registrationForm" validate="this">
    <t:errors/>
    <div>
        <label t:type="label" for="accountEmailAddress"/>
        <input t:type="textfield" t:id="accountEmailAddress" value="parentAccount.accountEmailAddress"/>
    </div>
    <div>
        <label t:type="label" for="accountFirstName"/>
        <input t:type="textfield" t:id="accountFirstName" value="parentAccount.accountFirstName"/>
    </div>
    <div>
        <label t:type="label" for="accountLastName"/>
        <input t:type="textfield" t:id="accountLastName" value="parentAccount.accountLastName"/>
    </div>
    <div>
        <label t:type="label" for="accountPassword"/>
        <input t:type="textfield" t:id="accountPassword" value="parentAccount.accountPassword"/>
    </div>
    <div>
        <label t:type="label" for="accountPasswordConfirmation"/>
        <input t:type="textfield" t:id="accountPasswordConfirmation" value="accountPasswordConfirmation"/>
    </div>
    <div><input type="submit" value="ok"/></div>
</form>
</body>
</html>

Unfortunately, even though I have annotated the entity with @NotNull annotations, those JSR-303 constraints are ignored.

Can anyone please help?

Regards,


Solution

  • I noticed that if I add a JSR-303 annotation to a property in the Tapestry class, it does get taken into account. Why then Tapestry won't take into account the annotation when placed upon a property of a JPA entity??

    In Tapestry a Form is accociated by a ValidationTracker that tracks all the provided user input and validation errors for every field in the form.

    It seems the default ValidationTrackerImpl doesn't care. You could provide your on tracker implementation via the Form's tracker parameter. I did something similiar with Struts2 to have my entities validated.

    Maybe (hopefully) annotating your parentAccount with @Valid (graph validation as it is called in the JSR-303 specification) also works (provided Tapestry cares).