제출 된 값은 validate()
구현에서 value
인수로 제공됩니다.
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
Object oldValue = ((UIInput) component).getValue();
if (value != null ? value.equals(oldValue) : oldValue == null) {
// Value has not changed.
return;
}
// Continue validation here.
}
대안은 ValueChangeListener
로 Validator
을 설계하는 것이다. 값이 실제로 변경된 경우에만 호출됩니다. 그것은 다소 해킹이지만, 당신이 정말로 필요로하는 일을합니다. 수동으로 추가 수동으로 무효하고 구성 요소를 모두 설정하고 상황에 직면해야하는 이유 당신이 거기 ValidatorException
을 던질 수 없습니다
<h:inputText ... valueChangeListener="#{uniqueValueValidator}" />
또는
@ManagedBean
public class UniqueValueValidator implements ValueChangeListener {
@Override
public void processValueChange(ValueChangeEvent event) throws AbortProcessingException {
FacesContext context = FacesContext.getCurrentInstance();
UIInput input = (UIInput) event.getComponent();
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
// Validate newValue here against DB or something.
// ...
if (invalid) {
input.setValid(false);
context.validationFailed();
context.addMessage(input.getClientId(context),
new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please enter unique value", null));
}
}
}
주와
<h:inputText ...>
<f:valueChangeListener binding="#{uniqueValueValidator}" />
</h:inputText>
, 즉 년대 구성 요소에 대한 메시지
context.validationFailed()
은 JSF가 업데이트 모델 값을 건너 뛰고 액션 단계를 호출하도록합니다.
가능한 [JSF/ADF 유효성 검사기에서 오래된 값을 얻는 방법] (http://stackoverflow.com/questions/2413424/how-to-get-old-value-from-jsf-adf-validator) – djmj