1

I am creating a form that posts data to a external url (authorizeNet DPM). I need jsf to validate the inputText prior to allowing the submission to happen. How do I get the validation rules to work in this case?

<form action="EXTERNAL_URL" method="POST">
   <h:inputText id="x_card_num" required="true" requiredMessage="A 16-digit credit card number is required.">
      <f:ajax event="change" render="@this" />  
      <f:validateLength maximum="20" minimum="3" />
      <f:validateRegex pattern="[0-9]{16}" />
   </h:inputText>
<h:commandButton value="Submit" id="submit"/> 
</form>
Sixthpoint
  • 1,161
  • 3
  • 11
  • 34

2 Answers2

1

JSF works on the server, so you always need to postback to your own server first. From there you could manually do a post to the target server (server to server communication). You could even capture the response of that server and stream it to the client.

Your client will see the URL of your server though, unless the target server responds with a redirect.

Mike Braun
  • 3,729
  • 17
  • 15
  • Would it be possible to redirect to that external server page after validation? The way authorizenet dpm works is you post to a url, it verifies and redirects you to another url, which then processes and finally returns you to your receipt page – Sixthpoint Dec 06 '13 at 17:58
  • @Sixthpoint yes, you can do a redirect, but it has to be a 307. A "normal" redirect is always GET based. POST redirects are rare and some browsers may complain (but test to be sure). BUT, consider the validation to be a convenience. The other server still cannot trust the request. – Mike Braun Dec 10 '13 at 17:53
  • I found this with code for this situation: http://stackoverflow.com/questions/4886661/post-call-to-another-server – Sixthpoint Dec 11 '13 at 16:37
1

I think you may need to mix JSF with normal JavaScript and make use of PrimeFaces's <p:commmandButton>.

<h:form>
   <h:inputText id="x_card_num" required="true" requiredMessage="A 16-digit credit card number is required.">
      <f:ajax event="change" render="@this" />  
      <f:validateLength maximum="20" minimum="3" />
      <f:validateRegex pattern="[0-9]{16}" />
   </h:inputText>
   <h:message id="msg" for="x_card_num" />

   <p:commandButton value="Submit" process="@this, x_card_num" update="msg"
                    oncomplete="if (!args.validationFailed) populatePostFormAndSubmit();" />
</h:form>

<form id="postForm" action="EXTERNAL_URL" method="POST">
    <input id="postInput" />
</form>

The process is:

  1. Users enter the card number in the JSF form and click Submit to start validating through an AJAX request.
  2. If there is no validation error, copy the card number from x_card_num input to postInput and submit the postForm using JavaScript.
Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90
  • Why PrimeFaces needed? Same can be done with RichFaces which OP already has (just need to change p --> a4j, process --> execute, update --> render, args.validationFailed --> #{facesContext.validationFailed}). – Andrey Dec 09 '13 at 09:15