普遍跳过所需输入的验证参数

use*_*738 7 jsf primefaces jsf-2

如何制作一些可以跳过web所需验证的按钮(但我仍然希望处理所有数据,所以立即等等不可能是真的).

重要的是它必须是普遍的.目前我在每个必需的现场条件下使用一些请求参数.代码示例如下

<p:inputText value="#{cc.attrs.data.exampleData1}" 
             required="#{param['onlySave'] == null}"/>
<p:inputText value="#{cc.attrs.data.exampleData2}" 
             required="#{param['onlySave'] == null}"/>
<p:inputText value="#{cc.attrs.data.exampleData3}" 
             required="#{param['onlySave'] == null}"/>

<p:commandButton value="Zapisz zmiany"
                 action="#{cc.attrs.controller.save()}"
                 update="@form">
   <f:param name="onlySave" value="true"/>
</p:commandButton>
Run Code Online (Sandbox Code Playgroud)

这个解决方案很好,因为我可以在每个页面中将此参数添加到按钮并跳过验证,但是当我的保存按钮没有进行任何重定向时,如果在保存方法中失败了一些java验证,我只是添加了一些没有重定向的消息然后我从输入中丢失了所有必需的样式.

验证失败时是否有可能在保存方法中设置onlySave参数null或者可能是更好的解决方案?

编辑: Balus回答很好,但是使用bean验证:

@Pattern(regexp = "^([^0-9]*)$", message = "only non numbers")
String field;
Run Code Online (Sandbox Code Playgroud)

它处理bean以外的所有数据.最好的只是忽略必需的字段属性,而不是验证等.

EDIT2:

<tr>
    <td class="label">
        <p:outputLabel id="label" for="#{cc.attrs.componentId}" value="#{cc.attrs.label}"/>
    </td>
    <td class="value">
        <cc:insertChildren/> --here component with componentId
    </td>
</tr>
<tr class="errorMessage">
    <td class="label"/>
    <td class="value">
        <p:message id="error" for="#{cc.attrs.componentId}" />
    </td>
</tr>
Run Code Online (Sandbox Code Playgroud)

use*_*738 0

到目前为止我想出的最好的解决方案是:

//p:label and p:message with property for=someId for each input above
<p:inputText id="someId" value="#{cc.attrs.data.exampleData1}" 
         required="#{param['onlySave'] == null}"/>
<p:inputText value="#{cc.attrs.data.exampleData2}" 
         required="#{param['onlySave'] == null}"/>
<p:inputText value="#{cc.attrs.data.exampleData3}" 
         required="#{param['onlySave'] == null}"/>

<p:commandButton value="save only"
             action="#{cc.attrs.controller.save()}"
             update="@(.ui-message,.ui-outputlabel)">
   <f:param name="onlySave" value="true"/>
</p:commandButton>

<p:commandButton value="accept button"
             action="#{cc.attrs.controller.accept()}"
             update="@form">
</p:commandButton>
Run Code Online (Sandbox Code Playgroud)

保存按钮仅更新所有错误和标签,因此我能够看到转换器错误等,但我不会丢失输入所需的样式。在这个例子中,我刚刚丢失了输入边框的红色。

如果您在必填字段的标签中使用 *,请不要更新 .ui-outputlabel