PrimeFaces' p:wizard validation not working
I have a p:wizard
with some tabs. In the first tab, the user selects a value (t:selectOneRadio
- I'm using Tomahawk). That's a required value.
If the user doesn't select a value, it won't go to t开发者_StackOverflow社区he next tab, but no validation error is displayed. Hm.
If the user has chosen a value, goes to the next tab, goes back to the first tab and chooses a different value, it will behave as if no value was chosen this time. (No validation error either, but the second tab can't be invoked).
And even worse: The user chooses a value in the first tab, goes to the second tab, tries invoke an action from there... a validation message appears; it acts as if no value was chosen in the first tab.
Is there any explanation for this?
UPDATE
The solution suggested in the PrimeFaces forum worked for me. (Adding process="@this" to the commandButton.)
Another thing you might want to consider..
If you are having trouble with required fields across tabs, you can manually perform your own validations between steps by implementing a flow event in your wizard component.
public String onFlowProcess(FlowEvent event) {
//First perform check to see if the user is attempting to remove the last visitor
if ("confirm".equals(event.getNewStep()) && (visitors == null || visitors.isEmpty())) {
log.debug("Validation failed for empty visitors");
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Visit must have at least one visitor.", null);
FacesContext.getCurrentInstance().addMessage(null, msg);
return event.getOldStep();
}
return event.getNewStep();
}
And the .xhtml file I declare the flowListener event. Try this and see if the validation messages go away.
<p:wizard showNavBar="true" widgetVar="scheduler" flowListener="#{scheduleVisit.onFlowProcess}" showStepStatus="true">
UPDATE
The solution suggested in the PrimeFaces forum worked for me. (Adding process="@this"
to the commandButton
.) No idea why, though!
This sounds like it could be one of a couple possible issues. You may be getting a validation error however you may not have declared your messages component correctly, or it is not getting updated. If this is the case their may be validation errors that you just don't see rendered on the page.
From the Primefaces Guide 2.2 for the Wizard component:
AJAX and Partial Validations - Switching between steps is based on ajax, meaning each step is loaded dynamically with ajax. Partial validation is also built-in, by this way when you click next, only the current step is validated, if the current step is valid, next tab’s contents are loaded with ajax. Validations are not executed when flow goes back.
The other problem may be that your property is not getting set properly in the managed bean and this is causing validation problems. This seems more likely.
I am having trouble thinking why one would need validation other than 'Required' for a selectOneRadio? You have a limited number of choices and one must be picked, therefore an incorrect value or invalid value should not be possible.
You just need to put the attribute required true on your radio button.
ex:
<p:selectOneRadio required="true" requiredMessage="you must put it">
<f:selectItem itemLabel="Días Laborales" itemValue="diasLab" />
<f:selectItem itemLabel="Días Ticados" itemValue="diasTic" />
</p:selectOneRadio>
PrimeFaces' p:wizard validation problem
Step :1 customerInformation.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<p:wizard flowListener="#{customerForm.onFlowProcess}">
<p:tab id="information" title="Information">
<p:panel header="Customer Information">
<h:outputLabel value="First Name" for="fName"/>
<h:inputText id="fname" value="#{customer.fname}"/>
<h:outputLabel value="Last Name" for="lName"/>
<h:inputText id="lname" value="#{customer.lname}"/>
</panel>
</p:tab>
<p:tab id="details" title="Details">
<p:panel header="Customer Details">
<h:outputLabel value="Address Line 1" for="addressOne"/>
<h:inputText id="addressOne" value="#{customer.addressOne}"/>
<h:outputLabel value="Address Line 2" for="addressTwo"/>
<h:inputText id="addressTwo" value="#{customer.addressTwo}"/>
</panel>
</p:tab>
</p:wizard>
</html>
Step:2 Create bean class CustomerForm.java
public class CustomerForm implements Serializable {
private static final long serialVersionUID = 1L;
private String fName;
private String lName;
private String addressOne;
private String addressTwo;
private static Logger logger = Logger.getLogger(CustomerForm.class.getName());
/**
* Call this method in p:wizard tag
*
*/
public String onFlowProcess(FlowEvent event) {
logger.info("Current wizard step:" + event.getOldStep());
logger.info("Next step:" + event.getNewStep());
return event.getNewStep();
}
/**
* @return the fName
*/
public String getfName() {
return fName;
}
/**
* @param fName the fName to set
*/
public void setfName(String fName) {
this.fName = fName;
}
/**
* @return the lName
*/
public String getlName() {
return lName;
}
/**
* @param lName the lName to set
*/
public void setlName(String lName) {
this.lName = lName;
}
/**
* @return the addressOne
*/
public String getAddressOne() {
return addressOne;
}
/**
* @param addressOne the addressOne to set
*/
public void setAddressOne(String addressOne) {
this.addressOne = addressOne;
}
/**
* @return the addressTwo
*/
public String getAddressTwo() {
return addressTwo;
}
/**
* @param addressTwo the addressTwo to set
*/
public void setAddressTwo(String addressTwo) {
this.addressTwo = addressTwo;
}
}
Note: don't put required="true" in xhtml file
精彩评论