0

I have a form with inputs to fill customer data with name surname and id

name     surname     lastName
[      ] [         ] [         ]

id       other fields
[      ] [           ]

Inputs are defined as usually:

<p:inputText id="name" required="true" value="#{contactForm.name}" label="Name" styleClass="form-control">
    <p:clientValidator/>
</p:inputText>
<p:message for="name" />

When id field is filled I make an ajax call to see if data corresponds with a customer in this way:

<p:outputLabel for="ID" value="ID" />
<p:inputText id="ID" required="true" value="ID" label="ID" styleClass="form-control">
    <p:ajax 
        event="change" 
        listener="#contactController.identifyCustomer(contactForm)}"
        process="@this name,surname1,surname2"
        update="newCustomerPanel" 
    />
    <p:clientValidator/>
</p:inputText>
<p:message for="dni" />

In the happy case user introduces all data in a correct way this works nice.

My problem comes when the ajax call is executed with no data retrieved and the user needs to change some of the inputs, if the event needs to be launched user must pass through id input field change data and return which is not the way.

Now is working with workaround: inserting an ajax call to all fields involved. When any input launches event, in server side I check if all data is complete and has changed to call the service that retrieves customer data...

But honestly, this is an ugly solution that wastes lot of calls and does not give a good user experience.

So I was wondering, is there a way group all fields and assign the ajax call and event="change" to this group?

Community
  • 1
  • 1
joc
  • 1,336
  • 12
  • 31
  • 1
    adding the generic java tag is not needed, it is even wrong. It indicates the problem can be reproduced in a plain java class with a main method and no external libraries other then the plain jdk. You can't here – Kukeltje Nov 24 '16 at 14:59
  • @Kukeltje not agree, primefaces fields need a java bean, my workaround works in a java controller and ajax call **is a java method** so... IMHO you're wrong... – joc Nov 24 '16 at 17:27
  • 1
    you code needs a servlet engine too, so you should tag it tomcat or whatever as well. It also needs a browser, operating system, electricity, etc... the fact that you *need* something written in this language, does not mean the problem is in that part and that is where the tagging is about. From the tag description: **_"Use this tag for questions referring to the Java programming language or Java platform tools."_**. Is your question related to the core programming language? Or to javac.exe or...? No, so the tag is not relevant. If you think it is managed-bean related, use that tag (or cdi) – Kukeltje Nov 24 '16 at 17:34

1 Answers1

1

Ajax tag can be used as a wapper for multiple components:

<f:ajax event="change">
  <h:input value... />
  <h:input value... />
  <h:input value... />
</f:ajax>
Esteban Rincon
  • 2,040
  • 3
  • 27
  • 44
  • that does not seem to work I get the error ` Event:change is not supported.` – joc Nov 24 '16 at 14:56
  • joc, `` and `` are not interchangeable. – BalusC Nov 24 '16 at 15:09
  • @BalusC thanks, now is working nice with data in all fields... but at the beginning with empty form, launches the ajax call when every field is filled... ¿there is some way to forbid the call until all mandatory fields inside `` have some data? – joc Nov 24 '16 at 17:23
  • Sure, use ajax on a commandButton... (ajax is implicit on a `p:commandButton`. Or you can add some 'onstart' javascript on each ajax that checks if the other fields have values and only then let the ajax call go through (return true) otherwise cancel it (return false) – Kukeltje Nov 24 '16 at 17:52
  • @Kukeltje thanks, I have 2nd solution working already, but is this the correct way? AFAIK I can't use first one because this fields are just a part of the form, user must fill more fields. I was wondering if there is an already jsf/primefaces built-in solution. – joc Nov 25 '16 at 08:21