Spring webflow partial validation

半世苍凉 提交于 2019-12-12 04:04:43

问题


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


回答1:


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'"
    



回答2:


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


来源:https://stackoverflow.com/questions/24637613/spring-webflow-partial-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!