2

Problem with validation

I would have run the validation when the method "registered"

In webflow: ...

<transition on="registered" to="registeredAction" bind="true" validate="true" />

...

My model looks like this:

class User {
private String name;
private String surname;
...
private List <address> addresses;
...
public void validateRegistered (ValidationContext context) {
Context.getMessageContext MessageContext messages = ();

if (name == null) {
messages.addMessage (new MessageBuilder (.) error (). source ("name".) code (MessageCodes.Error.REQUIRED.) build ());
}
}

In Address class

Class Address {

private String street;
private String city;

public void validateRegistered (ValidationContext context) {
Context.getMessageContext MessageContext messages = ();

if (street == null) {
messages.addMessage (new MessageBuilder (.) error (). source ("street".) code (MessageCodes.Error.REQUIRED.) build ());
}
}

Executing the action and gets errors in the validator for the User class, but not for Class Address

Anyone knows why this is so?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
tomasz-mer
  • 3,753
  • 10
  • 50
  • 69

2 Answers2

5

Spring will only call the validation on the bean set as the model for the view-state.

The following will only validate user:

<view-state id="something" view="something.jsp" model="user">
    <transition on="registered" to="registeredAction" bind="true" validate="true" />
</view-state>

You will need to create an object that encapsulates user and address and use it as as model (and call the validation method of User and Address in it's validation method).

Wilhelm Kleu
  • 10,821
  • 4
  • 36
  • 48
0

The other answer is correct about WebFlow only validating one model object, however you do not need to create another object. You can do nested validation. Just call the Address validator inside your User validator. Once you're inside your validator routine, you can bounce around and do pretty much whatever you need.

user3120173
  • 1,758
  • 7
  • 25
  • 39