【问题标题】:Universally skip validation params on required inputs普遍跳过所需输入的验证参数
【发布时间】:2015-09-26 08:28:12
【问题描述】:

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

重要的是它必须是通用的。目前,我在每个必需的字段条件中使用一些请求参数。下面的代码示例

<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>

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

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

编辑: Balus 的回答很好,但 bean 验证如下:

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

它处理 bean 超出该字段的所有数据。最好只忽略必需的字段属性,而不是验证等。

编辑2:

<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>

【问题讨论】:

  • 你的具体功能需求有点难以理解,但我相信你在找showcase.omnifaces.org/taghandlers/ignoreValidationFailed这是真的吗?
  • 哇,几乎完美,只有一个问题。当我使用此标签并在某些字段中写入例如非数字字段 bean 中的数字时:@Pattern(regexp = "^([^0-9]*)$", message = "numbers!!") 它不会处理数据,因为它应该是。但是所有其他字段都被处理。它甚至没有给我任何信息。
  • 那么,您的具体功能要求是从一个地方为给定类型的所有组件设置一个公共属性?也许你需要showcase.omnifaces.org/taghandlers/massAttribute
  • 当我点击 f:param 之类的特定按钮时,我是否能够更改大量参数的值?然后在自定义验证失败后,我可以在此特定按钮中使用的操作方法中再次更改其值吗?
  • 如果它是一个引用期望值的EL表达式,是的。

标签: jsf jsf-2 primefaces


【解决方案1】:

请参阅下面的修改代码以获取必填字段。您可以根据自己的要求将#{empty param.onlySave} 用于要跳过验证的字段。

<p:inputText value="#{cc.attrs.data.exampleData1}" 
             required="#{not empty param.onlySave}"/>
<p:inputText value="#{cc.attrs.data.exampleData2}" 
             required="#{not empty param.onlySave}"/>
<p:inputText value="#{cc.attrs.data.exampleData3}" 
             required="#{not empty param.onlySave}"/>

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

【讨论】:

  • 我看不出这和我的例子有什么区别
【解决方案2】:

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

//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>

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

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

【讨论】:

    【解决方案3】:

    我想出了一个解决方案:
    JSF 页面

    <h:form id="form" prependId="false">
                <c:if test="#{param['onlySave'] eq  true}">
                    <c:set  value="#{false}"  target="#{myBean}" property="required"/>
                </c:if>
                <p:inputText value="#{myBean.required}"/>
                <p:growl id="msgs" showDetail="true" />
                <p:inputText id="data1" value="#{myBean.data1}"   
                             required="#{myBean.required}" />
                <p:inputText  id="data2" value="#{myBean.data2}" 
                              required="#{myBean.required}"/>
                <p:inputText id="data3" value="#{myBean.data3}" 
                             required="#{myBean.required}"/>
    
                <p:commandButton value="Zapisz zmiany"
                                 action="#{myBean.save}" />
            </h:form>
    

    豆子

    @ManagedBean
    @ViewScoped
    public class MyBean implements  Serializable{
    
        private String data1, data2, data3;
        private boolean required;
    
        @PostConstruct
        public void init(){
            required = true;
        }
    
        public void save() {
            System.out.println(data1+", "+data2+", "+data3);
            required = false;
            //FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Info","Saved"));
           //  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("onlySave", "false");// FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("onlySave"));
        }
    
    // ... Getters & Setters
    }
    

    【讨论】:

    • 这就像巴鲁斯建议的那样。我会为 sessionScope 制作 MyBean 并在任何地方使用它。但它仍然不是那么通用,如果通过验证,则需要将此控制器注入应用程序中的每个控制器并在每个保存方法中实现状态。这是一些解决方案,但它并不简单(至少我相信:))
    • 如果你有一个模板,那么你可以在其中添加这部分&lt;c:if test="#{param['onlySave'] eq true}"&gt; &lt;c:set value="#{false}" target="#{myBean}" property="required"/&gt; &lt;/c:if&gt; 并使myBean 是会话范围的,那就不需要做提及的部分,你也会改变JSF框架你正在使用omnifaces
    • 因为 Balus 之前的建议是 smth,就像我说的它会强制在 100 个控制器中实现代码,它会花费我几天的时间来实现和测试。
    猜你喜欢
    • 2013-02-09
    • 1970-01-01
    • 2020-12-03
    • 2021-09-20
    • 2017-06-27
    • 1970-01-01
    • 2013-11-11
    • 2016-05-17
    • 1970-01-01
    相关资源
    最近更新 更多