开发者

Spring/Webflow problem with validation

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 (ne开发者_开发问答w 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?


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜