我有一個表格兩個輸入字段必須是一起驗證。我用這個BalusC tutorial作爲起點。錯誤驗證兩個inputText字段在一起
要求是一個邏輯XOR:必須填寫其中一個字段,但不能同時填寫。
也能正常工作的所有情況,除了這一個:
1)填寫的第一場,讓第二空白
2)移動到下一個頁面(價值得到保存)
3 )回到第一頁,刪除第一個字段並填寫第二個字段。
然後,我從我的驗證錯誤信息(「不要填寫這兩個字段」),但我刪除了第一。
下面是代碼:
<h:inputText id="bbvol" size="10" value="#{a6.behandlungsvolumen.wert}"
required="#{empty param['Laborwerte:pvol']}">
<f:convertNumber locale="de"/>
</h:inputText>
<h:inputText id="pvol" size="10" value="#{a6.plasmavolumen.wert}"
required="#{empty param['Laborwerte:bbvol']}">
<f:convertNumber locale="de"/>
<f:validator validatorId="volumeValidator" for="pvol"/>
<f:attribute name="bbv_value" value="Laborwerte:bbvol" />
</h:inputText>
VolumeValidator:
public class VolumeValidator implements Validator {
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
// Obtain the client ID of the first volume field from f:attribute.
String bbvolID = (String) component.getAttributes().get("bbv_value");
// Find the actual JSF component for the client ID.
UIInput bbvolInput = (UIInput) context.getViewRoot().findComponent(bbvolID);
// Get its value, the entered value of the first field.
Number bbvol = (Number) bbvolInput.getValue();
// Get the value of the second field
Number pvol = (Number) value;
// Check if only one of the fields is set
if (bbvol != null && pvol != null) {
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Do not fill both fields!", null));
}
}
}
調試這種情況表明,該行
Number bbvol = (Number) bbvolInput.getValue();
仍持有舊值,而不是新的。
我試過用getSubmittedValue()
而不是getValue()
和這個工程。但Javadoc中此方法表示:
此方法應該僅由 解碼中使用()和驗證此 組分()方法,或它的相應 呈現器。
我不知道我是否可以使用此方法,而不會在以後運行問題。
所以我的問題是:
,這是什麼問題的原因是什麼?
是使用getSubmittedValue()
代替getValue()
一個真正的解決方案?
謝謝BalusC。我會嘗試這個,並報告它是否適合我。這聽起來像是一個老問題:「這是一個錯誤還是一個特徵?」 ;-) – 2011-04-27 16:29:49
它適合我。 – BalusC 2011-04-27 16:31:08
它的工作原理!非常感謝! – 2011-04-28 07:28:34