我有一个表格两个输入字段必须是一起验证。我用这个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