JSF中至少需要一个/一组多个字段 [英] At least one/group of multiple fields required in JSF

查看:45
本文介绍了JSF中至少需要一个/一组多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行一个搜索功能,该功能具有多个字段,并且至少需要其中一个或一组.

I'm trying to do a search function of which I have multiple fields and need at least one of them required, or a group of them.

我在StackOverflow上找到的唯一答案就是这个:

The only answer I found on StackOverflow is this one: How to validate if at least one of multiple input fields is entered?

这里有2种解决方案:一种使用Omnifaces(由于项目要求而不能使用),另一种并不优雅,因为当字段数很大时,它会增长很多.

And there are 2 solutions there: One uses Omnifaces (which I can't use due to project requirements), and the other is not elegant, because it grows a lot when the number of fields is big.

例如,我有以下字段:

                <p:selectOneMenu value="#{backingBean.type}"id="type">
                    <f:selectItem itemLabel="#{constantsController.SELECT_TYPE_HEADER}"
                        itemDisabled="true" />
                    <f:selectItems
                        value="#{constantsController.getTypes().entrySet()}"
                        var="entry" itemValue="#{entry.key}" itemLabel="#{entry.value}" />
                </p:selectOneMenu>

                <p:inputText id="number" value="#{backingBean.number}"/>

                <p:inputText id="year" value="#{backingBean.year}"/>

                <p:inputText id="number2" value="#{backingBean.number2 }"/>

                <p:selectOneMenu value="#{backingBean.state}"
                    id="state" filter="true" filterMatchMode="startsWith">
                    <f:selectItem itemLabel="Choose its state" disabled="true" />
                    <f:selectItems value="#{constantsController.getStates().entrySet()}"
                        var="entry" itemValue="#{entry.key}" itemLabel="#{entry.value}" />
                </p:selectOneMenu>

                <p:selectOneMenu id="entity"
                    value="#{backingBean.entity }">
                    <f:selectItem itemLabel="Select entity"
                        itemDisabled="true" />
                    <f:selectItem itemLabel="Entity1" itemValue="1"></f:selectItem>
                    <f:selectItem itemLabel="Entity2" itemValue="2"></f:selectItem>
                </p:selectOneMenu>

                <p:selectOneMenu id="documentType"
                    value="#{backingBean.documentType}">
                    <f:selectItems
                        value="#{constantsController.getDocumentTypes().entrySet()}"
                        var="entry" itemValue="#{entry.key}" itemLabel="#{entry.value}" />
                </p:selectOneMenu>
                <p:inputText id="documentNumber" value="#{backingBean.documentNumber}"/>

如您所见,我有多个字段.并且要求表明我需要以下要求之一(我将写出ID):

So as you can see, I have multiple fields. And the requirements dictate that I need one of the following required (I'll write the id's):

1-类型+数字+年份

1- type + number + year

2-数字2 +状态

3-实体

4- documentType + documentNumber

4- documentType + documentNumber

因此,当我单击commandButton时,需要使用它来检查是否填充了这些组之一(1、2、3或4).

So I when I click the commandButton, I need this to check if one of these groups (1, 2, 3 or 4) is filled.

此外,还需要团队本身,也就是说,我已经填写了实体",并且我也填写了年份",它不能让我前进,因为年份需要伴随着类型"和数字".

Also, the groups in itself need to be required, i.e. let's say I already filled 'entity', and I also filled the 'year', it cannot let me advance since year needs to be accompanied by 'type' and 'number'.

推荐答案

我通过将后备Bean中的布尔值与每个字段/字段组关联来解决此问题.这不是一个优雅的解决方案,但是我没有找到其他解决方案.

I solved it by associating a boolean in the backing bean to each of the fields/group of fields. It's not an elegant solution, but I wasn't finding any other solution.

所以我所做的是通过后备bean中的字段组定义一个用true值初始化的布尔值:

So what I did was define by group of fields in the backing bean a boolean initialized with true value:

private boolean typeNumberYear = true;
private boolean number2State = true;
private boolean entityRequired = true;
private boolean documentTypeDocumentName = true;
private boolean masterRequired = true;

然后验证它们:

public void checkRequiredFields(){

    if(!type.equals("") && number != 0 && year != 0)
        typeNumberYear = false;
    if(number2 != 0 && !state.equals(""))
        number2State = false;
    if(!entity.equals(""))
        entityRequired= false;
    if(!documentType.equals("") && documentNumber != 0)
        documentTypeDocumentName = false;

    if(!typeNumberYear || !number2State || !entityRequired || !documentTypeName)
        masterRequired = false;
}

由于所有这些都在后备bean中,所以我可以将这个功能放在命令按钮的actionListener上,而将动作功能仍放在控制器中.

Since all this is in the backing bean, I can just put this function on the actionListener of the command button, while the action function is still in the controller.

<p:commandButton value="Search" icon="ui-icon-search"
                actionListener="#{backingBean.checkRequiredFields}"
                action="#{controller.searchButtonClicked}"/>

现在在required参数中,我只将各自的布尔值和masterRequire关联到所有它们.例如.在typenumberyear输入中,我将所需内容如下:

Now in the required parameter I just associated the respective boolean and the masterRequired on all of them. E.g. in the type, number and year inputs, I put the required as follows:

required = "backingBean.typeNumberYear and backingBean.masterRequired"

该解决方案的唯一缺点是,在界面中,上面指示的所有字段上都会出现一个星号,但是例如,如果您仅输入实体,它将按预期进行搜索,然后它将更新并删除星号.

The only downside to this solution, is that in the interface, there will be appearing an asterisk on all the fields that are indicated above, but if you just input the entity for example, it will search as it is supposed to, and then it will update and remove the asterisks.

这篇关于JSF中至少需要一个/一组多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆