Java 如何根据另一个字段来验证所需的字段';接缝中的s值?

Java 如何根据另一个字段来验证所需的字段';接缝中的s值?,java,hibernate,validation,annotations,seam,Java,Hibernate,Validation,Annotations,Seam,我正在尝试为我的项目创建一个简单的自定义验证器,但我似乎找不到一种让seam有条件地进行验证的方法 以下是我得到的: 助手/支持bean(不是实体) 我需要的是,当且仅当selected为true时,需要“start”和“end” 我尝试在类型目标上创建一个自定义验证器,但seam似乎不想选择它并验证它。(可能是因为它不是一个实体?) 以下是我的自定义注释的基本思想: @ValidatorClass(RequiredIfSelectedValidator.class) @Target(Eleme

我正在尝试为我的项目创建一个简单的自定义验证器,但我似乎找不到一种让seam有条件地进行验证的方法

以下是我得到的:

助手/支持bean(不是实体)

我需要的是,当且仅当selected为true时,需要“start”和“end”

我尝试在类型目标上创建一个自定义验证器,但seam似乎不想选择它并验证它。(可能是因为它不是一个实体?)

以下是我的自定义注释的基本思想:

@ValidatorClass(RequiredIfSelectedValidator.class)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiredIfSelected {
    String message();
}

public class RequiredIfSelectedValidator implements Validator<RequiredIfSelected>, Serializable {
  public boolean isValid(Object value) {
    AdSiteHelper ash = (AdSiteHelper) value;
    return !ash.isSelected() || (ash.getStart() && ash.getEnd());
  }
  public void initialize(RequiredIfSelected parameters) { }
}
@validator类(RequiredIfSelectedValidator.class)
@目标(ElementType.TYPE)
@保留(RetentionPolicy.RUNTIME)
public@interface RequiredIfSelected{
字符串消息();
}
公共类RequiredIfSelectedValidator实现可序列化的验证器{
公共布尔值无效(对象值){
adsiteheloper ash=(adsiteheloper)值;
return!ash.isSelected()| |(ash.getStart()&&ash.getEnd());
}
public void initialize(requiredifsselected参数){}
}

我也遇到了类似的问题。如果持有这些值的Bean总是相同的,那么您可以使用

//Assuming you have the @Name annotation populated on your Bean and a Scope of CONVERSATION or higher
AdSiteHelper helper = (AdSiteHelper)Component.getInstance("adSiteHelper");
同样,当您使用Seam时,您的验证器也不需要如此复杂。你不需要一个接口,它可以简单到

@Name("requiredIfSelectedValidator")
@Validator
public class RequiredIfSelectedValidator implements javax.faces.validator.Validator {
   public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
      //do stuff
   }
}

我也有一个类似的问题。如果持有这些值的Bean总是相同的,那么您可以使用

//Assuming you have the @Name annotation populated on your Bean and a Scope of CONVERSATION or higher
AdSiteHelper helper = (AdSiteHelper)Component.getInstance("adSiteHelper");
同样,当您使用Seam时,您的验证器也不需要如此复杂。你不需要一个接口,它可以简单到

@Name("requiredIfSelectedValidator")
@Validator
public class RequiredIfSelectedValidator implements javax.faces.validator.Validator {
   public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
      //do stuff
   }
}