简体   繁体   English

<h:form target=“_blank” only after validation

[英]<h:form target=“_blank” only after validation

I want a form that after submitting and validating opens a new tab with the results. 我想要一个表单,在提交和验证后打开一个包含结果的新选项卡。 I used the <h:form target="_blank" method to get the new tab, but it shows the validation errors in the new tab. 我使用<h:form target="_blank"方法获取新选项卡,但它在新选项卡中显示验证错误。 I want it to only open the new tab if the validation is passed. 如果验证通过,我希望它只打开新选项卡。

My guess is the only real cross browser solution would be to do some SJAX validation onsubmit, but I'm not sure if there are any libraries that will do this for me. 我的猜测是唯一真正的跨浏览器解决方案是在提交时做一些SJAX验证,但我不确定是否有任何库可以为我做这个。

So, is there a way to validate and open a new window only if validation passes? 那么,只有在验证通过后才有办法验证和打开新窗口吗?

Since you haven't provided any code sample I assume you are using a <h:form> and a <h:commandButton> within it. 由于您没有提供任何代码示例,我假设您在其中使用<h:form><h:commandButton>

If so, you have to do a simple trick to overcome this issue. 如果是这样,你必须做一个简单的技巧来克服这个问题。 You have to use a <a4j:commandButton> instead of your <h:commandButton> and keep the <h:commandButton> hidden. 您必须使用<a4j:commandButton>而不是<h:commandButton>并隐藏<h:commandButton> Then use the oncomplete event of the <a4j:commandButton> to determine whether to click the <h:commandButton> . 然后使用<a4j:commandButton>oncomplete事件来确定是否click <h:commandButton>

The <h:commandButton is clicked by javascript, only if validations are passed. 仅当验证通过时,才会通过javascript单击<h:commandButton

<h:form target="_blank">
  <h:inputText value="" required="true" id="txt"/>
  <rich:message for="txt"/>
  <a4j:commandButton value="Submit" action="#{bean.someMethod()}"           
    oncomplete="if(#{!(facesContext.maximumSeverity.ordinal ge 2)}){
    #{rich:element('btn')}.click();}"/>
  <h:commandButton value="temp" id="btn" style="display:none;"/>
</h:form>

If you can invoke the validation using JavaScript using some AJAX behavior , you may be able to do it easily by using an onSubmit event function as below: 如果您可以使用JavaScript使用某些AJAX行为来调用验证 ,则可以使用onSubmit事件函数轻松地执行此操作,如下所示:

   function fnSubmit(){
     var validated= performValidation();
     if(validated){
         document.getElementById('formName').target="_blank";
         document.getElementById('formaName').submit();
     }
   }

I've been inpired by this thread to found a solution for my problem. 我已经受到这个线程的启发,为我的问题找到了解决方案。 So I decided to post mine, too. 所以我决定也发布我的。 I had the requirement to stay on the same site if an error occured and if not to open a new tab with a page for printing. 如果发生错误,我有要求留在同一站点,如果没有打开带有打印页面的新选项卡。

<h:form target="#{printBean.isErrorHappened() ? '_self' : '_blank'}"
     id="formPrintSearchResult">
     <h:commandLink id="cmdLinkPrintSearchResult"
          value="Print overview"
          onmouseover="#{rich:element('checkForErrors')}.click();"
          action="#{printController.printSearch()}" />
      <a4j:commandLink id="checkForErrors" style="display:none;" 
          render="formPrintSearchResult" />
</h:form>   

If you like you could include another action on the a4j:commandLink to check for errors and set the errorHappened in the printBean. 如果您愿意,可以在a4j:commandLink上包含另一个操作来检查错误并在printBean中设置errorHappened。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM