[英]How to let the required attribute of h:inputText depend on certain set of values from h:selectOneMenu?
我正在使用JSF 2.0。
我的页面上有一个h:selectOneMenu
,它包含一个值列表,并且在同一页面上有一个h:inputText
,其required
应取决于当前所选的h:selectOneMenu
值。 只有一组特定值才能触发required
检查,其他值则不会。
这是我尝试过的:
<h:inputText ... required="#{(selectedPaymentType.value == 'some value') || (selectedPaymentType.value == 'other value')}" />
在上面的代码中, #{selectedPaymentType}
定义在h:selectOneMenu binding
。
还有3个这样的值,它们应该将required
属性触发为true
。 这看起来有点笨拙。 有没有更好的方法呢?
如果在您的设计中可以使用enum作为您的PaymentType,其中包含一些类似的界面
public interface RequireSomeMoreStuff {
public boolean required();
}
public enum PaymentType implements RequireSomeMoreStuff {
FOO { boolean required() { return true; } },
BAR { boolean required() { return false; } }
}
Fant已经给出了正确方向的暗示,你应该使用具有required
属性的枚举,但似乎你并不完全确定如何正确实现它。 不可否认,Fant的答案不够详细。 所以这里有一个更精细的答案。
基本上,您需要用枚举替换所有下拉值,如下所示:
public enum PaymentType {
FOO("Some label for foo", true),
BAR("Some label for bar", false),
BAZ("Some label for baz", true);
private String label;
private boolean required;
private PaymentType(String label, boolean required) {
this.label = label;
this.required = required;
}
public String getLabel() {
return label;
}
public boolean isRequired() {
return required;
}
}
并按如下方式使用它
<h:selectOneMenu binding="#{selectedPaymentType}" value="#{bean.selectedPaymentType}">
<f:selectItems value="#{bean.availablePaymentTypes}" var="paymentType"
itemValue="#{paymentType}" itemLabel="#{paymentType.label}" />
</h:selectOneMenu>
<h:inputText ... required="#{selectedPaymentType.value.required}" />
同
private PaymentType selectedPaymentType; // +getter+setter
public PaymentType[] getAvailablePaymentTypes() {
return PaymentType.values();
}
(或者如果您使用的是OmniFaces,请使用<o:importConstants>
,那么<f:selectItems>
就不需要这样的getter;不,在任何情况下都不需要转换器,JSF / EL有已经内置的枚举转换)
请参阅, required
属性现在更加简化,因为它已在与所选值关联的模型中定义。
只有在选择了selectOneMenu
某些值时,我才会使用a4j
来渲染inputText:
<h:selectOneMenu value="#{MyBean.selectedValue}">
<f:selectItems value="#{MyBean.listOfValues}" />
<a4j:support event="onchange" reRender="requiredText" ajaxSingle="true"/>
</h:selectOneMenu>
<a4j:outputPanel id="requiredText">
<h:inputText value="#{MyBean.inputValue}" rendered="#{(MyBean.selectedValue == value_1) || (MyBean.selectedValue == value_2) || ...}" />
</a4j:outputPanel>
为了避免在inputText的rendered
参数上使用大字符串,我建议您创建一个执行以下条件的布尔函数: rendered="#{MyBean.inputTextIsRendered}
。
还有一个基于Validators的解决方案。 这是我的方法:
JSF代码
上面的selectOneMenu
代码使用binding
标记将selectOneMenu
选择的值与将在为inputText
组件声明的validator方法中检索的属性绑定。
然后,您需要创建CheckInputValue
验证器类。
public class CheckInputValue implements Validator {
public CheckInputValue() {}
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
//We retrieve thei binded value:
UIInput confirmComponent = (UIInput) component.getAttributes().get("selectedValue");
String theValue = confirmComponent.getSubmittedValue();
//Here you check the retrieved value with the list of values that makes your inputText required.
//In these cases you will chech the inputText is not empty and, if so, you return a ValidatorException:
if(isEmpty){
FacesMessage message = new FacesMessage();
message.setDetail("inputText cannot be empty");
message.setSummary("inputText cannot be empty");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}
}
}
最后,您必须在faces-config.xml
声明您的验证器类:
<validator>
<validator-id>CheckInputValue</validator-id>
<validator-class>myPackage.myValidations.CheckInputValue</validator-class>
</validator>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.