Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use one button to calculate and validate via javascript and then submit and navigate
    primarykey
    data
    text
    <p>I have a js-function <code>codeAddress()</code> which completes an address via google maps javascript api and sets a global js-variable <code>valid = true</code>, if the full address was recognized. It also updates the value of a <code>&lt;p:inputText&gt;</code> with the completed address.</p> <p>Now I have one <code>&lt;p:commandButton&gt;</code> that I want to</p> <ol> <li>execute <code>codeAddress()</code></li> <li>if <code>valid = true</code> submit form so address in <code>&lt;p:inputText&gt;</code> is saved in <code>addressBean</code> and</li> <li>navigate to next page</li> <li>if <code>valid = false</code> trigger jsf-validator messages</li> </ol> <p>Unfortunately my setup still does not work with <code>valid</code> as the essential value to determine if next page. It seems, that the value is submitted to bean too late.</p> <p>this is my jsf code:</p> <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui"&gt; &lt;h:head&gt; &lt;script src="https://maps.googleapis.com/maps/api/js?v=3&amp;amp;sensor=false"&gt;&lt;/script&gt; &lt;/h:head&gt; &lt;h:body&gt; &lt;h:outputScript name="jsf.js" library="javax.faces" target="head"/&gt; &lt;h:outputScript library="js" name="bugMaps.js" /&gt; &lt;body onload="initialize()" /&gt; &lt;h:form id="addressForm" onsubmit="codeAddress();"&gt; &lt;p:inputText id="address" required="true" validatorMessage="Please enter a valid address." validator="#{addressBean.validate()}"&gt; &lt;p:ajax update=":addressForm:addressValidate" /&gt; &lt;/p:inputText&gt; &lt;h:message id="addressValidate" for=":addressForm:address"/&gt; &lt;p:commandButton value="submit" onclick="codeAddress();"/&gt; &lt;p:inputText id="fullAddress" value="#{addressBean.fullAddress}" /&gt; &lt;p:inputText id="valid" value="#{addressBean.valid}" /&gt; &lt;/h:form&gt; &lt;/h:body&gt; &lt;/html&gt; </code></pre> <p>this is the bugMaps.js:</p> <pre><code>var geocoder; var map; var valid; function initialize() { geocoder = new google.maps.Geocoder(); } function codeAddress() { var address = (document.getElementById('addressForm:address').value + ", Germany"); geocoder.geocode({'address' : address},function(results, status) { if (status == google.maps.GeocoderStatus.OK) { var latLong = results[0].geometry.location; var latitude = results[0].geometry.location.lat(); var longitude = results[0].geometry.location.lng(); var country, postal_code, locality, street_number, route; for (i = 0; i &lt; results[0].address_components.length; ++i) { var component = results[0].address_components[i]; if (!locality &amp;&amp; component.types.indexOf("locality") &gt; -1) locality = component.long_name; else if (!postal_code &amp;&amp; component.types.indexOf("postal_code") &gt; -1) postal_code = component.long_name; else if (!country &amp;&amp; component.types.indexOf("country") &gt; -1) country = component.long_name; else if (!street_number &amp;&amp; component.types.indexOf("street_number") &gt; -1) street_number = component.long_name; else if (!route &amp;&amp; component.types.indexOf("route") &gt; -1) route = component.long_name; } if (typeof latLong != "undefined" &amp;&amp; typeof latitude != "undefined" &amp;&amp; typeof longitude != "undefined" &amp;&amp; typeof route != "undefined" &amp;&amp; typeof street_number != "undefined" &amp;&amp; typeof postal_code != "undefined" &amp;&amp; typeof locality != "undefined" &amp;&amp; typeof country != "undefined"){ valid = true; } else{ valid=false; }; document.getElementById('addressForm:fullAddress').value = results[0].formatted_address; jsfSubmit(); } else{ alert('Geocode was not successful for the following reason: '+ status); valid=false; } }); }; function navigateToAnotherPage(data){ if(valid){ document.location.href='nextPage.xhtml'; } }; function jsfSubmit(){ jsf.ajax.request(this, event, {execute:"@form", onevent:navigateToAnotherPage}); alert("nothing"); // FIXME: without this useful alert bean keeps old values and display them on nextPage }; </code></pre> <p>this is my AddressBean.java:</p> <pre><code>package hoho.main.managebean; import java.io.Serializable; import javax.enterprise.context.SessionScoped; import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.component.UIInput; import javax.faces.context.FacesContext; import javax.faces.validator.ValidatorException; import javax.inject.Named; import com.sun.faces.util.MessageFactory; @Named @SessionScoped public class AddressBean implements Serializable { private static final long serialVersionUID = 1L; private String fullAddress; private boolean valid = false; public void validate(FacesContext context, UIComponent toValidate, Object value) { if(!valid){ String client = toValidate.getClientId(context); FacesMessage msg = MessageFactory.getMessage(UIInput.REQUIRED_MESSAGE_ID, client); throw new ValidatorException(msg); } } public String getFullAddress() { return fullAddress; } public void setFullAddress(String fullAddress) { this.fullAddress = fullAddress; } public boolean isValid() { return valid; } public void setValid(boolean valid) { this.valid = valid; } } </code></pre> <p>this is the nextPage.xhtml:</p> <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui"&gt; &lt;h:head&gt; &lt;/h:head&gt; &lt;h:body&gt; &lt;h:outputText value="#{addressBean.fullAddress}" /&gt; &lt;/h:body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload