<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<f:view>
<h:head>
</h:head>
<h:body>
<div style="margin-left: auto; margin-right: auto; width: 300px;">
<h1>User Manager</h1>
<h:form id="user-input-form">
<p:panelGrid id="input"
layout="grid"
columns="2">
<p:outputLabel for="user-name" value="User name" />
<p:inputText id="user-name"
value="#{debugUserManager.selectedEntity.name}"
required="true"
style="width: 125px;">
</p:inputText>
<p:outputLabel for="password" value="Password" />
<p:password id="password"
value="#{debugUserManager.selectedEntity.password}"
required="true"
redisplay="true"
style="width: 125px;">
</p:password>
</p:panelGrid>
<p:messages severity="error" />
</h:form>
<h:form id="user-button-form">
<p:commandButton id="save-button"
icon="fa fa-save"
value="Save"
action="#{debugUserManager.save()}"
process="@form :user-input-form"
update="@form :user-input-form"
style="margin-top: 10px;" />
<p:growl id="growl"
showSummary="true"
showDetail="false"
sticky="false"
life="10000" />
</h:form>
</div>
</h:body>
</f:view>
</html>
Here's the manager bean:
@Named
@ViewScoped
public class DebugUserManager implements Serializable
{
private static final long serialVersionUID = 1L;
private SimpleUser selectedEntity;
@PostConstruct
public void init()
{
this.selectedEntity = new SimpleUser();
}
public SimpleUser getSelectedEntity()
{
return selectedEntity;
}
public void setSelectedEntity( SimpleUser selectedEntity )
{
this.selectedEntity = selectedEntity;
}
public void save()
{
System.out.println( "Saving simple user: " + selectedEntity.getName() + "//" + selectedEntity.getPassword() );
FacesContext.getCurrentInstance().addMessage( "user-button-form:growl", new FacesMessage( "User successfully saved. User name = " + selectedEntity.getName() + ", password = " + selectedEntity.getPassword() ) );
}
public class SimpleUser implements Serializable
{
private static final long serialVersionUID = 1L;
private String name;
private String password;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}
}
Have a look at the structure. I use two forms, one for the inputs and one for the button(s). This example stems from a bigger context.
In the save button, I use process="@form :user-input-form"
to submit both forms.
I'd expect the validation to kick in on the first form to block the action from being called, but this isn't happening. Instead the action is called and because the first form isn't submitted, any value entered into the inputs will end up as null on the entity:
QUESTION:
Is this expected behavior??
If Y: Is this specified anywhere? What's the logical explanation?
If N: Is it a bug in Mojarra? Is this a bug in PrimeFaces? Anything else that could be wrong? ♂️
I've tried PrimeFaces versions 6.2, 7.0 and 8.0-SNAPSHOT, but all of them expose the described problem.