Spring webflow partial validation

2019-09-10 15:41发布

I am trying to implement partial validation in Spring Webflow 2.4. According to their reference manual the validation should be done very simply using groups:

@NotNull
@Size(min = 2, max = 30, groups = State1.class)
private String name;

In my understanding the State1 should be the ID of view-state in which the model should be validated. So the definition of this view state in flow.xml would look like this:

<view-state id="state1" model="modelObject"/>

I was trying to define the State1 as an inner class of my model object, but without success.

The Webflow reference doesn't provide full manual for partial validation, so my question is: Am I missing something? Does anybody have experience with using the partial validation using JSR303 groups?

Thanks, Shimon

2条回答
Lonely孤独者°
2楼-- · 2019-09-10 16:22

"In my understanding the State1 should be the ID of view-state in which the model should be validated."

Here groups is not referring to view-state id. It is an inner class or parent or interface implemented by model object.

To realize JSR-303 partial validations, in SWF 2.4 onwards(this is the version SWF starts supporting it), you need to specify validation-hints as:

    <view-state id="someView" model="modelObject" validation-hints="group1,group2">

where group1, group2 can be inner Class either in the model type modelObject or its parent types or interfaces implemented by modelObject.

As per the documentation here:

Each hint can be an inner Class either in the model type or its parent types. 
For example, given org.example.MyModel with inner type Group1 and Group2 you 
can specify the hints "group1", "group2" or both "group1,group2". A hint can 
also be a fully qualified class name. The hint "default" indicates the default
validation group, i.e. javax.validation.groups.Default. Also, the validation-hints 
property can be an expression that resolves to a String or an Object[] containing    
Class based hints.
查看更多
beautiful°
3楼-- · 2019-09-10 16:33

I think I can answer my own question now :)

The root of the problem was in 2 things:

  1. The Group1 should be an inner interface of model object. So the model object class should look something like this:

    public clas ModelObject{
    
      @NotEmpty(groups=Group1.class)
      private String field1;
    
      public interface Group1{}
    }
    
  2. the name od validation-hint should be in single quotes

    validation-hints="'group1'"
    
查看更多
登录 后发表回答