从< h:inputText>获取空字符串.经过验证

从< h:inputText>获取空字符串.经过验证

问题描述:

有没有一种方法可以使jsf验证程序处理空字符串? 我为所有inputTexts提供了一个自定义验证器. 与其也不必实施"required"标记,我希望让客户处理程序处理空字符串并在后备bean中确定该字段是否为必填字段.

Is there a way to make jsf validator handle empty strings? I have a custom validator for all inputTexts. Instead of having to implement the "required" tag as well I would like to have the customer handler handle the empty strings and determine within the backing bean if that field is mandatory or not.

<h:inputText id="SSID_STR" styleClass="propertyInput" value="#{wifiDM.SSIDStr}" validator="#{wifiDM.validate}" >
    <h:message for="SSID_STR" fatalClass="mandatoryFieldMissing" tooltip="true" />
</h:inputText>

如果必须添加必需的标签,则必须对应用程序中的每个输入文本执行此操作:

If I had to add the required tag I would have to do this for every input text in my app:

<h:inputText id="SSID_STR" styleClass="propertyInput" value="#{wifiDM.SSIDStr}" validator="#{wifiDM.validate}" required="#{wifiDM.isRequired} requiredMessage="*">
    <h:message for="SSID_STR" fatalClass="mandatoryFieldMissing" tooltip="true" />
    <f:attribute name="InputID" value="SSID_STR">
</h:inputText>

这真是一团糟...

更新(澄清) 首先,使用JSF 1.2. 每个字段在需要与否方面都是独立的,因此组件相关性的问题无关紧要. 我的目标是通过验证器传递空值,因此我不必将两个标签都添加到inputText中.

Update (Clarification) First of all, using JSF 1.2. Each field is independent in the sense of being required or not so the issue of component dependency is irrelevant. My goal was to pass the null value through the validator so I don't have to add both tags to the inputText.

此外,Validator处理程序将"component"作为参数,这意味着我可以获取其ID并实时实时查看是否需要. (不一定总是需要相同的组件).

Also, The Validator handler gets 'component' as a parameter, which means I can get its ID and see in real time if it is required. (The same component may not always be required).

如果我使用required="#{bean.requireCheck}"(例如),我能以某种方式获得调用它的组件吗?

If I use required="#{bean.requireCheck}" (For Example) can I somehow get the component from which it was called?

默认情况下,JSF 1.x不会在空字段上触发验证器.为此,必须使用required属性.如果您的具体问题是每当填写第一个字段时都必须将第二个字段设置为required,那么您只需在required属性中进行检查即可.

JSF 1.x does by default not fire validators on empty fields. For that the required attribute has to be used. If your concrete problem is that the second field has to be set required whenever the first field is been filled in, then you should just check that so in the required attribute.

<h:inputText value="#{bean.input1}" binding="#{input1}" />
<h:inputText value="#{bean.input2}" required="#{not empty input1.value}" />

如果这不是功能要求,那么您应该在问题中进行更多说明,以便给出更合适的答案.

If that's not the functional requirement, then you should clarify that more in the question so that a better suited answer can be given.

更新:因此,您想从模型开始控制需求?如下所示,将Map<String, Boolean>添加到Bean中:

Update: So you want to control the requireness from the model on? Add a Map<String, Boolean> to the bean as follows:

private Map<String, Boolean> required;

public Bean() {
    required = new HashMap<String, Boolean>();
    required.put("foo", true);
    required.put("bar", false); // false entries are by the way not necessary.
    // ...
}

public Map<String, Boolean> getRequired() {
    return required;
}

按以下方式使用它:

<h:inputText id="foo" required="#{bean.required['foo']}" />
<h:inputText id="bar" required="#{bean.required['bar']}" />

如果您使用的是JSF 2.0,则可以按照以下步骤进行操作

If you were using JSF 2.0, you could do it as follows

<h:inputText id="foo" required="#{bean.required[component.id]}" />
<h:inputText id="bar" required="#{bean.required[component.id]}" />


与具体问题无关,将<h:message>用作<h:inputText>的子代是非法的.将它们彼此相邻.


Unrelated to the concrete problem, it's illegal to put a <h:message> as child of <h:inputText>. Put them next to each other.