facesContext.validationFailed is coming back null - jsf-2

For what reason facesContext.validationFailed could be null? (in a postBack where there are errors..)
I had to resort to an ugly if (#{fn:containsIgnoreCase(facesContext.maximumSeverity,'ERROR')}) showSecurePopup('confirm'); because facesContext.validationFailed is coming back null..
the test code is:
<h:form>
<h:messages />
<br></br>
<h:outputText value="Validation failed: #{facesContext.validationFailed}" />
<br></br>
<h:outputLabel for="field1" value="Test Field" />
<br></br>
<h:inputText required="true" />
<h:commandButton action="#{contactBacking.submitComment}" value="Submit"
<f:ajax render="#form" execute="#form" />
</h:commandButton>
</h:form>
EDIT: Edited as per #BalusC's answer.. and took screenshot of result:

The FacesContext#isValidationFailed() returns a boolean which is never null. Just get rid of that nonsensicial nullcheck.
<h:outputText value="Validation failed: #{facesContext.validationFailed}" />

Related

Omnifaces ValidateEqual Does not perform validation

I am trying out the Omnifaces validators especially the validateEqual and so I created a test page such as this.
<p:messages autoUpdate="true" showDetail="false" />
<h:form id="registerForm" prependId="false">
<p:panelGrid columns="2" styleClass="register-grid">
<h:outputLabel for="password" value="Password *" />
<p:inputText id="password" value="" label="Password"
requiredMessage="Password is required" size="30">
<f:validateRequired />
</p:inputText>
<h:outputLabel for="confirmPassword" value="Confirm Password *"
requiredMessage="Confirm Password is required" />
<p:inputText id="confirmPassword" value="" label="Confirm Password" requiredMessage="Confirm password is required"
size="30">
<f:validateRequired />
</p:inputText>
<o:validateEqual components="password confirmPassword" message="Passwords are not equal"/>
<f:facet name="footer">
<p:commandButton value="Register" action="/pages/public/login"/>
<p:commandButton value="Cancel" immediate="true" action="/pages/public/login"/>
</f:facet>
</p:panelGrid>
</h:form>
Not sure but nothing is happening and I see from firebug below error.
<partial-response>
<error>
<error-name>class javax.faces.component.UpdateModelException</error-name>
<error-message>/pages/public/register.xhtml #26,57 value="": Illegal Syntax for Set Operation</error-message>
</error>
<changes>
<extension ln="primefaces" type="args">{"validationFailed":true}</extension>
</changes>
</partial-response>
What could be the cause?
/pages/public/register.xhtml #26,57 value="": Illegal Syntax for Set Operation
This is basically telling that it's not possible to perform a "set" operation (a setter method call) on an empty value expression.
Either remove the value attribute altogether (at least from the "confirm" field), or specify a valid value expression such as value="#{bean.password}" (at least for the first field). So, basically:
<p:inputText id="password" value="#{bean.password}" label="Password"
requiredMessage="Password is required" size="30" required="true" />
<p:inputText id="confirmPassword" label="Confirm Password"
requiredMessage="Confirm password is required" size="30" required="true" />
<o:validateEqual components="password confirmPassword"
message="Passwords are not equal" />
This has nothing to do with using <o:validateEqual>. You'd have exactly the same problem when not using it. You may however want to use OmniFaces FullAjaxExceptionHandler in order to get a real error page on an exception during an ajax request instead of complete lack of visual feedback.

Password matching message in primefaces

I copied the exact code from this Primefaces http://blog.primefaces.org/?p=1512 blog about easy password validation
<h:outputLabel for="pwd1" value="Password 1: *" />
<p:password id="pwd1" value="#{registerMB.password}"
feedback="false" match="pwd2" label="Password 1" required="true" />
<h:outputLabel for="pwd2" value="Password 2: *" />
<p:password id="pwd2" value="#{registerMB.password}"
feedback="false" label="Password 2" required="true" />
<f:facet name="footer">
<p:commandButton value="Register" action="/pages/public/login" />
<p:commandButton value="Cancel" immediate="true"
action="/pages/public/login" />
</f:facet>
Validation works but I am only able to get the Validation Error. The message Password 1 should match Password 2 is never displayed. Is there anymore configuration for this?
I have Primefaces 3.4.1 downloaded
Try to add the following
validatorMessage atribute inside p:password tag id="pwd2":
<p:password id="pwd2" value="#{registerMB.password}"
feedback="false" label="Password 2" required="true"
validatorMessage="password 1 should match password 2"/>
add p:message tag to show the error below the h:form tag
<p:messages id="messages" showDetail="true" autoUpdate="true"/>
add <p:messages id="messages" showDetail="true" autoUpdate="true"/>
just like int Primefaecs Password Showcase
<h:form id="form">  
<p:panel header="Match Mode">
<p:messages id="messages" showDetail="true" autoUpdate="true"/>
<h:panelGrid columns="2" id="matchGrid">
<h:outputLabel for="pwd1" value="Password 1: *" />
<p:password id="pwd1" value="#{passwordBean.password5}" match="pwd2" label="Password 1" required="true"/>
<h:outputLabel for="pwd2" value="Password 2: *" />
<p:password id="pwd2" value="#{passwordBean.password5}" label="Password 2" required="true"/>
</h:panelGrid>
<p:commandButton id="saveButton" update="matchGrid" value="Save" />
</p:panel>
</h:form>  

Show/hide JSF2 form using <p:selectBooleanButton>

I need to show and hide component in my JSF 2-PrimeFaces application, please find my code below for the same:
<p:outputLabel for="online_offer" value="Online offer#{msg._question_mark}" styleClass="font-size-1em font-weight-bold input-panel-main" />
<p:panel>
<h:panelGrid columns="1">
<p:selectBooleanButton id="online_offer" value="#{QckPstBen.offer.isExpired}" onLabel="Yes" offLabel="No" onIcon="ui-icon-check" offIcon="ui-icon-close" >
<f:ajax event="click" render="#form" />
</p:selectBooleanButton>
</h:panelGrid>
</p:panel>
<h:outputLabel value=" " />
<h:panelGroup rendered="#{QckPstBen.offer.isExpired}">
<p:outputLabel for="website" value="Website/link to the offer#{msg._colon}" styleClass="font-size-1em font-weight-bold input-panel-main" />
<p:panel>
<h:panelGrid columns="1">
<p:inputText id="website" required="true" size="38" />
<p:watermark for="website" value="www.discountbox.in" />
</h:panelGrid>
</p:panel>
</h:panelGroup>
But it doesnt work, any clue
PrimeFaces components doesn't support <f:ajax>. Use <p:ajax> instead.
<p:selectBooleanButton ...>
<p:ajax update="#form" />
</p:selectBooleanButton>
Note that I omitted the event attribute, it defaults here to click already.
Place rendered on children of <h:panelGroup> instead or place a container of some sort around it, for example p:panel.
This may be OBE by now but...
When referencing the page bean, don't I recall in the #{} context, the convention of using lowercase for class names and method/attributes of the page bean (i.e. not #{QckPstBen.offer.isExpired} but #{qckPstBen.offer.isExpired}?

JSF form value change does not work

I have a form with 4 fields (2 dates, 2 selectOne).
My Bean is SessionScoped.
When I change the values in the form once, and click on submit, the applications works fine.
But when I want to change the values in form again, nothing happens. no change nor execution.
My Form looks like:
<ui:define name="sidebar">
<p:panel header="Select Values">
<h:form id="graphform">
<p:growl id="msg" />
<p:panelGrid columns="1">
<h:outputLabel for="servervalue" value="Servervalue: " />
<p:selectOneMenu value="#{graph.servervalue }" id="servervalue">
<f:selectItems value="#{formconst.SERVERVALUES}" />
</p:selectOneMenu>
<h:outputLabel for="startdate" value="Startdate: " />
<p:calendar disabledWeekends="false" size="10" pattern="dd.MM.yyyy"
showOtherMonths="true" maxlength="10"
mindate="#{formconst.MINDATE }" maxdate="#{formconst.MAXDATE}"
showButtonPanel="true" navigator="true" readOnlyInputText="true"
id="startdate" value="#{graph.startdate }" showOn="button"
required="true" />
<h:outputLabel for="enddate" value="Enddate: " />
<p:calendar disabledWeekends="false" size="10" pattern="dd.MM.yyyy"
showOtherMonths="true" maxlength="10"
mindate="#{formconst.MINDATE }" maxdate="#{formconst.MAXDATE }"
showButtonPanel="true" navigator="true" readOnlyInputText="true"
id="enddate" value="#{graph.enddate }" showOn="button"
required="true" />
<h:outputLabel for="filter" value="Filter: " />
<p:selectOneMenu value="#{graph.filter }" id="filter">
<f:selectItems value="#{formconst.GENERALFILTER}" />
</p:selectOneMenu>
</p:panelGrid>
<p:separator />
<p:panelGrid columns="2">
<p:commandButton value="Submit" update="#all"
action="#{ graph.updateChart }" />
<p:commandButton value="Reset" update="#all"
action="#{ graph.resetChart }" />
</p:panelGrid>
</h:form>
</p:panel>
</ui:define>
<ui:define name="content">
<h:form id="linechart">
<p:lineChart value="#{graph.cartesianChartBean.chartmodel }"
enhancedLegend="true" legendPosition="nw" xaxisAngle="50" />
</h:form>
</ui:define>
The same happens when I change SessionScoped to ViewScoped or RequestScoped.
I can change the values only one time.
I use primefaces 3.2 with apache myFaces 2.0.2 on WebSphere Application Server 8.0
Best Regards Veote

header containing h:link and h:commandLink wont work?

I have this weird situation. I have a jsf 2.0 application, with template pages.
on the header I have some links with h:link to keep the url bookmarkability and h:commandLink that I use to signin and signout.. the problem is when I navigate to a specific page via h:link
let say the url become bla.jsf?uName=dino then once I'm in bla.jsf?uName=dino I click the signout button that should take me to signout.jsf wont work. in stead, it takes me to bla.jsf page without ?uName=dino.
I have no nested h:form and everything seems normal to me. is there any suggestion why its behaving like this ?
I'm sure you are asking if I have h:form on h:commandLink. yes I do have that too.here is my code:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<div class="floatRight">
<h:form id="firstFormHeader" prependId="false">
<p:autoComplete id="searchInputId" size="40" style="z-index: 20;" value="#{autoComplete.text}" completeMethod="#{autoComplete.complete}"/>
<h:commandButton value="#{label.search}" action="#{autoComplete.getSearchedVideo}" />
<p:watermark for="searchInputId" value="#{label.search}" />
</h:form>
</div>
<div style="z-index: 1;" id="topsection">
<div style="position:relative; bottom:-51px;">
<div id="menu" style="height:37px;" class="title ui-widget-header">
<h:panelGrid border="0" columns="2" style="float: left;">
<h:column>
<h:panelGrid columns="2">
<h:column>
<h:form id="secondFormHeader" prependId="false" >
<h:selectOneMenu id="videoGenereHeaderId" value="#{homePageVideoLoader.videoCategory}" required="true" onchange="submit()" valueChangeListener="#{homePageVideoLoader.getSelectedCatagoryList}">
<f:selectItem id="item0" itemLabel="#{select.category}" itemValue="" />
<f:selectItem id="item1" itemLabel="#{select.animation}" itemValue="23" />
<f:selectItem id="item2" itemLabel="#{select.autos}" itemValue="15" />
<f:selectItem id="item3" itemLabel="#{select.comedy}" itemValue="10" />
<f:selectItem id="item4" itemLabel="#{select.documentary}" itemValue="19" />
<f:selectItem id="item5" itemLabel="#{select.drama}" itemValue="4" />
<f:selectItem id="item6" itemLabel="#{select.education}" itemValue="16" />
<f:selectItem id="item7" itemLabel="#{select.hiphop}" itemValue="5" />
<f:selectItem id="item8" itemLabel="#{select.guragigna}" itemValue="12" />
<f:selectItem id="item9" itemLabel="#{select.news}" itemValue="6" />
<f:selectItem id="item10" itemLabel="#{select.oldies}" itemValue="7" />
<f:selectItem id="item11" itemLabel="#{select.reggae}" itemValue="8" />
<f:selectItem id="item12" itemLabel="#{select.harari}" itemValue="13" />
<f:selectItem id="item13" itemLabel="#{select.oromigna}}" itemValue="14" />
<!-- <f:selectItem id="item7" itemLabel="#{select.entertainment}" itemValue="ent" />
<f:selectItem id="item8" itemLabel="#{select.gaming}" itemValue="gam" />
<f:selectItem id="item9" itemLabel="#{select.health}" itemValue="hea" />
<f:selectItem id="item10" itemLabel="#{select.howto}" itemValue="how" /> -->
<f:selectItem id="item14" itemLabel="#{select.music}" itemValue="3" />
<f:selectItem id="item15" itemLabel="#{select.politics}" itemValue="18" />
<!-- <f:selectItem id="item13" itemLabel="#{select.nonprofit}" itemValue="non" />
<f:selectItem id="item14" itemLabel="#{select.blog}" itemValue="blo" />
<f:selectItem id="item15" itemLabel="#{select.animals}" itemValue="anm" />
<f:selectItem id="item16" itemLabel="#{select.science}" itemValue="sci" />
<f:selectItem id="item17" itemLabel="#{select.sport}" itemValue="spo" />
<f:selectItem id="item18" itemLabel="#{select.travel}" itemValue="tra" /> -->
</h:selectOneMenu>
</h:form>
</h:column>
<h:column>
<h:outputText rendered="#{userAuthentication.userLogged and userAuthentication.user.usersValid}" value="#{label.hello} "/>
<h:link rendered="#{userAuthentication.userLogged and userAuthentication.user.usersValid}" value="#{userAuthentication.user.usersFirst}" outcome="userhome?uName=#{userAuthentication.user.usersUname}">
</h:link>
</h:column>
</h:panelGrid>
</h:column>
</h:panelGrid>
<h:panelGrid border="0" columns="2" styleClass="menuPostion">
<h:column>
<h:link outcome="home" value="#{label.home}" /> |
<h:link outcome="upload" value="#{label.upload}" /> |
<h:form id="thridFormHeader" prependId="false" >
<h:commandLink rendered="#{userAuthentication.userLogged}" value="#{label.signout}" action="#{userAuthentication.doSignOut}" immediate="true"/>
</h:form>
<h:link rendered="#{!userAuthentication.userLogged}" value="#{label.createAccount}" outcome="registration" />
<h:outputText rendered="#{!userAuthentication.userLogged}" value=" | "/>
<h:link rendered="#{!userAuthentication.userLogged}" value="#{label.signin}" outcome="signin" />
</h:column>
<h:column>
<h:form prependId="false" id="languageForm">
<h:selectOneMenu value="#{language.localeCode}" onchange="submit()"
valueChangeListener="#{language.countryLocaleCodeChanged}">
<f:selectItems value="#{language.countriesInMap}" />
</h:selectOneMenu>
</h:form>
</h:column>
</h:panelGrid>
</div>
</div>
</div>
</ui:composition>
The second <h:form> is the one that does not work.
Update: sorry is not working. it is not even going to doSignOut method..
once I reach the page bla.jsf?uName=dino
and see the source code regarding the signout link it shows me
<form id="j_idt31" enctype="application/x-www-form-urlencoded" action="/myapp/content/bla.jsf" method="post" name="j_idt31" target="">
...
and obviously bla.jsf gives me error message because it's waiting for uName to have a value.
why it is doing so? is it a bug ?
Also, you might have noticed that I have h:selectOneMenu that send data to the bean when the value changes, but once I'm on bla.jsf?uName=dino and try to change the value of h:selectOneMenu it wont work for it does not redirected to the bean for some reason so is h:commandLink yet h:link(s) work fine.. how can I can resolve it BalusC.. Thank you so much for taking the time and answering by the way
The <h:commandLink> is nested inside a <h:form> whose HTML-generated <form action> URL defaults to the current request URL. It's basically a link which uses JavaScript to submit that form. As the form's action defaults to the current request URL, you don't see a change in the request URL. Technically, it's working perfectly fine.
If you want to change the request URL after submit, then you need to send a redirect. You can do that by adding faces-redirect=true parameter to the outcome value:
public String doSignOut() {
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return "signout?faces-redirect=true";
}
Or when you're still using the old fashioned <navigation-case> in faces-config.xml, then you need to add a <redirect /> entry.
with the help of great BalusC I was able to find my solution. my code was not working because it was menubar so every time the page changes the header or the menubar stay the same which means the form wont be changed.
I do not know why but rather than using h:link I used h:commandLink with combination of f:setPropertyActionListener to send the uName, then on the server side I did this
public String goToUserHomePage(){
methodNM = CLASS_NAME+".goToUserHomePage()";
log.info("in " + methodNM);
System.out.println("in " + methodNM);
System.out.println("in " + methodNM+"-------------------------"+user.getUsersUname());
return "userhome?uName="+user.getUsersUname()+"&faces-redirect=true";
}
by doing so every links whether with h:commandLink or h:link works fine

Resources