when jsf validation failed, ajax is skipped - validation

I have this code :
<p:inputText id="abc" value="#{myBean.name}" >
<f:validator validatorId="mandatoryInput" />
<p:ajax event="change" update="..." listener="#{myBean.myFunction()}" />
</oct:inputTextUpper>
When the Validator throw a new ValidatorException, is skipped. Is it possible to launch myFunction() even if validation is failed ?
Thanks :)

That's the default JSF behavior. As a work around you could work with f:event:
<p:inputText id="abc" value="#{myBean.name}">
<f:validator validatorId="mandatoryInput" />
<p:ajax event="change" update="..." listener="#{myBean.myFunction}" />
<f:event type="preRenderComponent" listener="#{myBean.callMyFunction}" />
</p:inputText>
In MyBean add the following method:
public void callMyFunction(ComponentSystemEvent e) {
UIInput input = (UIInput) e.getComponent();
if (!input.isValid()) {
myFunction();
}
}

Related

How to get the response from a bean in jsf ajax call using f:ajax

Hi i am working on jsf f:ajax i it worked for me when i have done the validations with in the page using ajax. but now i wanted to get the response from a Managed bean method that is called on as a ajax listener.
I m not able to get the response back in any of the format.any body please help me.
Here is the code
register.xhtml
<h:outputText value="Full Name:" escape="false" />
<h:inputText value="#{display.user}" required="true" label="User ID"
id="username">
<f:validateRegex pattern="[A-Za-z]*[\.][A-Za-z]*"></f:validateRegex>
<!-- <f:validator validatorId="com.jason.jsf.custom.Myvalidator"></f:validator> -->
<f:ajax render="usernameMessage outputMessage" event="blur" />
</h:inputText>
<h:message id="usernameMessage" for="username" style="color:red;" />
<h:outputText value="Email:" escape="false" />
<h:inputText value="#{display.email}" required="true" id="EmailID"
label="Email ID"
validatorMessage="#{display.email} is not valid email">
<f:validateRegex
pattern="[\w\.-]*[a-zA-Z0-9_]#[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]" />
<f:ajax render="EmailIDmsg" event="blur"
listener="#{display.getMessage}" onevent="fun" />
</h:inputText>
<h:message id="EmailIDmsg" for="EmailID" />
script.js
function fun(e){
var xmlDoc,x,txt,i;
alert("text"+e.responseText);
alert("XML"+e.responseXML);
}
Method in Display.java a managed bean with name display contians a field email with all the setters and getters
public String getMessage(AjaxBehaviorEvent e) throws InterruptedException {
System.out.println("hai this is listener method");
System.out.println(e.getComponent().getId());
System.out.println(email);
return email;
}
I using JSF 2.0 Mojarra 2.0.3

selectOneButton change listener always returning null

I'm using JSF 2 along with primefaces, i have a selectOneButton which have 2 values, EN/FR, i want to be notified each time the language is changed, and then change the locale of the page, i have set valueChangeListener and an ajax event,
but everytime the change listener is fired, the new value is always null!
here is the xhtml code:
<h:form id="f">
<p:selectOneButton id="lang" value="#{currentUser.language}" valueChangeListener="#{currentUser.languageChanged}" >
<f:selectItem itemLabel="English" itemValue="en" />
<f:selectItem itemLabel="Françcais" itemValue="fr" />
<f:ajax event="change"/>
</p:selectOneButton>
</h:form>
the languageChanged method for testing:
public void languageChanged(ValueChangeEvent event) {
System.out.println("new val: " + event.getNewValue());
System.out.println("old val: " + event.getOldValue());
}
i even tried to attach to the ajax a listener:
<f:ajax event="change" listener="#{currentUser.languageChanged}"/>
and the method:
public void languageChanged(AjaxBehaviorEvent event) {
SelectOneButton button = (SelectOneButton)event.getComponent();
System.out.println(button.getValue()) ;
}
Is this the wrong way to retrieve the new selected value from a p:selectOneButton with ajax?
UPDATE:
So i know what is causing this problem
actually my selectOneButton is inside a menuItem which is inside a splitButton, when i take the selectOneButton out side the splitButton it works fine!
why is the fact that the selectOneButton is inside the menuItem preventing it from send it to the server?
here the code:
<h:panelGroup style="float: right;" >
<h:form id="f">
<p:splitButton value="#{currentUser.fullName}" icon="ui-icon-person" styleClass="toTheRight" style="min-width: 200px;" >
<p:menuitem value="Matricule: #{currentUser.matricule}" />
<p:menuitem value="#{msgs.role}: #{currentUser.role}" />
<p:separator />
<p:menuitem>
<p:selectOneButton id="lang" value="#{currentUser.language}" valueChangeListener="#{currentUser.languageChanged}" >
<f:selectItem itemLabel="English" itemValue="en" />
<f:selectItem itemLabel="Françcais" itemValue="fr" />
<f:ajax event="change"/>
</p:selectOneButton>
</p:menuitem>
<p:separator />
<p:menuitem value="#{msgs.settings}" icon="ui-icon-wrench" />
<p:menuitem value="#{msgs.logout}" icon="ui-icon-arrowthickstop-1-w" url="/j_spring_security_logout"/>
</p:splitButton>
</h:form>
</h:panelGroup>
I have a similiar snap-code and that should work, here is what i have:
<p:outputLabel for="scriptMigrate" value="Migrate" style="font-weight:bold" />
<p:selectOneButton id="scriptMigrate" value="#{scriptConfiguration.scriptMigrate}" required="true">
<f:selectItem itemLabel="Yes" itemValue="1" />
<f:selectItem itemLabel="No" itemValue="0" />
<p:ajax update="fsstobe" event="change" listener="#{scriptConfiguration.updateForm()}" />
</p:selectOneButton>
The update componetent is just for me to know what to render or not in the rest of the page, but here is the method:
public void updateForm() {
System.out.println("entered with value: " + scriptMigrate);
if (scriptMigrate.equals("1")) {
renderScriptAsIs = true;
}
if (scriptMigrate.equals("0") || scriptMigrate.equals("")) {
renderScriptAsIs = false;
}
}
So i assume when you change the value you just want to call the method languageChanged(), so maybe my example helps, i have no arguments, try without them first to see if it helps.
Could you try
<p:ajax listener="#{currentUser.languageChanged}"/>
and remove the valueChangeListener from the selectOneButton.
In languageChanged(AjaxBehaviorEvent event) just read the new value from the language-property.
<p:selectOneButton value="#{languageBean.languageName}">
<p:ajax listener="#{languageBean.changeLanguage()}" partialSubmit="true"/>
<f:selectItem itemLabel="DE" itemValue="#{CONST.LANGUAGE_DE}"/>
<f:selectItem itemLabel="EN" itemValue="#{CONST.LANGUAGE_EN}" />
</p:selectOneButton>
and in the method from bean:
public void changeLanguage() {
System.out.println(languageName);
}

JSF 2: access selected value of selectOneMenu in a form BEFORE submit

VIEW
<h:form id="main_form">
<p:inputText id="title" required="true" label="Title" value="#{myBean.myLink.title}" immediate="true" />
<p:selectOneMenu id="scope" required="true" label="Scope" value="#{myBean.myLink.scope}" immediate="true" >
<f:selectItem itemLabel="Please choose" itemValue="" />
<f:selectItems value="#{myBean.availableScopes}" id="selScope"/>
</p:selectOneMenu>
<p:inputText id="link" required="true" label="URL" value="#{myBean.myLink.link}" immediate="true">
<p:ajax event="blur" update="msgLink" listener="#{myBean.checkUrl}" />
</p:inputText>
... msgLink and other (required) elements
... submit button
</h:form>
Managed Bean
#Component("myBean")
#Scope("session")
public class MyBean implements Serializable {
private Link myLink;
private Map<String, String> availableScopes;
public MyBean() {
this.availableScopes = new HashMap<String, String>();
this.availableScopes.put("Intranet", "Intranet");
this.availableScopes.put("Internet", "Internet");
}
// setter/getters etc.
public void checkUrl() {
System.out.println(myLink.getTitle()); // works
System.out.println(myLink.getScope()); // DOES NOT work
System.out.println(myLink.getLink()); // works
}
}
I want to check the URL depending of the selected scope before submitting the form. But the called method can access the inputText values of the object. Not the value chosen in selectOneMenu.
I have tried it with getExternalContext().getSessionMap().get("scope") but the SessionMap is null at that time.
Any chance to access the selected value of the combo box?
Thanks
Jim
The <p:ajax> (and <f:ajax>) inside an UIInput component executes/processes by default the current UIInput component (#this) only, not others.
If you want to execute/process all those UIInput components when the listener method is to be invoked, then you should specify that as such in <p:ajax process> (or <f:ajax execute>) attribute:
<p:inputText id="title" ... />
<p:selectOneMenu id="scope" ... >
...
</p:selectOneMenu>
<p:inputText id="link" ...>
<p:ajax process="title scope link" ... />
</p:inputText>
Unrelated to the concrete problem, I wonder how all those immediate="true" attributes are useful in this context. Are you certain you need them?

AjaxBehaviorEvent primefaces3.2

My problem is that the following code returns null but if I look when debuging source then submittedValue has the correct date.
Java:
public void changeOneMenuP(AjaxBehaviorEvent event) {
String id = (String) event.getComponent().getAttributes().get("value");
if(id != null) {
listEntity = escDao.findByxxxx(id, true);
}
}
XHTML:
<h:selectOneMenu id="idSelect" immediate="true" style="width:120px" value="#EntityBB.idUni}" label="#{bundleComunes.unidad}">
<f:selectItem itemLabel="#{bundleComunes.seleccionar}..." itemValue="" />
<f:selectItems value="#{configuracionBB.listEntity}" var="lUni" itemValue="#lUni.id}" itemLabel="#{lUni.desc}" />
<p:ajax event="change" update="sisArm" listener="#{entityBB.changeOneMenuP}" />
</h:selectOneMenu>
Any Idea????
better don't mix primefaces p:ajax with Pure JSF
try using f:ajax
<f:ajax event="change" render="sisArm" listener="#{entityBB.changeOneMenuP}" />
Also , you better access your id like this
public void changeOneMenuP(AjaxBehaviorEvent event) {
listEntity = escDao.findByxxxx(getidUni(), true);//or just idUni
}

gmap PrimeFaces p:ajax

I am trying to update the google map of PrimeFaces with the new latitude, longitude with the help of p:ajax, but it is not working...I'm using JSF 2.0. I have used p:ajax earlier in the similar way and it worked beautifully. Any idea why this does not work? The following is the code, contForm is the id of the form.
<h:outputText value="Latitude :"/>
<h:inputText value="#{confirmBrandRegistration.newBrand.mapLatitude}" size="10">
<p:ajax event="blur" update="contForm:gMapID"/>
</h:inputText>
<h:outputText value=" Longitude :"/>
<h:inputText value="#{confirmBrandRegistration.newBrand.mapLongitude}" size="10" >
<p:ajax event="blur" update="contForm:gMapID"/>
</h:inputText>
<h:outputText value=" Marker :"/>
<h:inputText value="#{confirmBrandRegistration.newBrand.mapMarker}" size="20" >
<p:ajax event="blur" update="contForm:gMapID"
listener="#{confirmBrandRegistration.updateMarker}"/>
</h:inputText>
</h:panelGrid>
<p:outputPanel id="gMapID">
<f:view contentType="text/html">
<p:gmap center="#{confirmBrandRegistration.newBrand.mapLatitude}, #{confirmBrandRegistration.newBrand.mapLongitude}"
zoom="16" type="HYBRID" streetView="true"
model="#{confirmBrandRegistration.simpleModel}"
style="width:500px;height:400px" />
</f:view>
</p:outputPanel>
I solved it right now:)
index page:
<h:panelGroup id="pmap">
<p:inputText value="#{mapManager.address}" label="Adresa" />
<h:outputText value="#{mapManager.geo}" id="m" />
<p:commandButton value="OK" actionListener="#{mapManager.updateMapCenter(ae)}" update="pmap" />
<p:gmap center="#{mapManager.geo}"
zoom="7"
type="HYBRID"
style="width:800px;height:400px"
streetView="true"
model="#{mapManager.map}"
overlaySelectListener="#{mapBean.onMarkerSelect}"
>
<p:gmapInfoWindow>
<p:outputPanel style="text-align:center; display:block; margin:auto:">
<h:outputText value="#{mapManager.marker.title}" />
wserw
</p:outputPanel>
</p:gmapInfoWindow>
</p:gmap>
</h:panelGroup>
part of managed bean:
private MapModel map;
private Marker marker;
private String address;
private String geo="49.817491999999992, 15.4729620";
public MapManager() {
}
#PostConstruct
public void init() {
events = edao.findAll();
map = new DefaultMapModel();
for (Event event : events) {
Marker m=new Marker(new LatLng(event.getLat(), event.getLng()), event.getName());
map.addOverlay(m);
}
}
public void updateMapCenter(ActionEvent ae) {
GMapService gs=new GMapService();
LatLng geo=gs.getGeocode(address);
this.geo=geo.getLat()+","+geo.getLng();
}
hope it solved your problem.

Resources