2011-10-23 69 views
3

一個ConstraintValidator我有一個簡單的驗證器來驗證一個字符串值是一個預定義列表的一部分:如何創建列表

public class CoBoundedStringConstraints implements ConstraintValidator<CoBoundedString, String> 
{ 

private List<String> m_boundedTo; 

@Override 
public void initialize(CoBoundedString annotation) 
{ 
    m_boundedTo = FunctorUtils.transform(annotation.value(), new ToLowerCase()); 
} 

@Override 
public boolean isValid(String value, ConstraintValidatorContext context) 
{ 
    if (value == null) 
    { 
     return true; 
    } 

    context.disableDefaultConstraintViolation(); 
    context.buildConstraintViolationWithTemplate("should be one of " + m_boundedTo).addConstraintViolation(); 
    return m_boundedTo.contains(value.toLowerCase()); 
} 

} 

例如,它會驗證:

@CoBoundedString({"a","b" }) 
public String operations; 

我想創建一個驗證器對於一個字符串列表來驗證這樣的事情:

@CoBoundedString({"a","b" }) 
public List<String> operations = new ArrayList<String>(); 

我試過這個:

public class CoBoundedStringListConstraints implements ConstraintValidator<CoBoundedString, List<String>> 
{ 

private CoBoundedString m_annotation; 

@Override 
public void initialize(CoBoundedString annotation) 
{ 
    m_annotation = annotation; 
} 

@Override 
public boolean isValid(List<String> value, ConstraintValidatorContext context) 
{ 
    if (value == null) 
    { 
     return true; 
    } 

    CoBoundedStringConstraints constraints = new CoBoundedStringConstraints(); 
    constraints.initialize(m_annotation); 
    for (String string : value) 
    { 
     if (!constraints.isValid(string, context)) 
     { 
      return false; 
     } 
    } 
    return true; 
} 

} 

的問題是,如果列表中包含2個或更多illeagal值,會出現只有一個(第一個)約束違反。我希望它有不止一個。我應該怎麼做?

+0

我發現了一個可能的解決方案:http://stackoverflow.com/questions/4308224/hibernate-validation-of-collections-of -primitives。但是,沒有設法使其工作。 – oshai

回答

3

有2個問題,當前的代碼:

在你CoBoundedStringListConstraintsisValid方法,你應該遍歷像這樣給出的列表中的所有元素(設置allValid標誌適用):

@Override 
public boolean isValid(List<String> value, 
     ConstraintValidatorContext context) { 
    if (value == null) { 
     return true; 
    } 

    boolean allValid = true; 
    CoBoundedStringConstraints constraints = new CoBoundedStringConstraints(); 
    constraints.initialize(m_annotation); 
    for (String string : value) { 
     if (!constraints.isValid(string, context)) { 
      allValid = false; 
     } 
    } 
    return allValid; 
} 

第二個是執行equals違反約束(javax.validation.Validator.validate()返回一組!)。當你總是輸入相同的信息(should be one of [a, b])時,該集合仍然只包含1​​個元素。作爲解決方案,你可以將電流值,預先準備的消息(類CoBoundedStringConstraints):

@Override 
public boolean isValid(String value, ConstraintValidatorContext context) { 

    if (value == null) { 
     return true; 
    } 

    if (!m_boundedTo.contains(value)) { 
     context.disableDefaultConstraintViolation(); 
     context.buildConstraintViolationWithTemplate(
       value + " should be one of " + m_boundedTo) 
       .addConstraintViolation(); 
     return false; 
    } 
    return true; 
} 
+0

謝謝!有用! '@ Valid'假設要做'CoBoundedStringListConstraints'呢? – oshai