Primefaces validation message - jsf

How can I display custom validation message in using prime faces message tag?
My code:
<p:messages id="message" />
<p:inputText id="updatedescription"
value="#{equipTemplateBean.equipmentSpecificationsVO.description}"
requiredMessage="Please enter Description"
required="true"/>

Related

JSF 2 - RenderKitUtils renderUnhandledMessages [duplicate]

I have a JSF 2.1 + PrimeFaces 3.3 page:
<h:form id="particluar">
<p:message id="msg" globalOnly="true" display="text"/>
<h:panelGrid columns="2" id="test">
<h:panelGrid id="panel2" columns="3">
<p:inputText id="name1" value="Student.studentID" required="true"/>
<p:commandButton value="Check Name" actionListener="#{Student.abc}" process="name1" update="panel2" />
<p:message id="msg1" for="name1" display="text"/>
</h:panelGrid>
<p:inputText id="name2" required="true"/>
<p:message id="msg2" for="name2" display="text"/>
<p:inputText id="name3" required="true"/>
<p:message id="msg3" for="name3" display="text"/>
</h:panelGrid>
<p:commandButton value="submit" actionListener="#{Student.xyz}" update="particluar" />
</h:form>
I'm trying to manually add faces messages when the name is invalid:
public void xyz() {
if (name1.equals("primefaces")) {
FacesContext.getCurrentInstance().addMessage("msg1", new FacesMessage(FacesMessage.SEVERITY_ERROR,"Invalid Name", "Invalid Name"));
}
if (name2.equals("primefaces")) {
FacesContext.getCurrentInstance().addMessage("msg2", new FacesMessage(FacesMessage.SEVERITY_ERROR,"Invalid Name", "Invalid Name"));
}
if (name3.equals("primefaces")) {
FacesContext.getCurrentInstance().addMessage("msg3", new FacesMessage(FacesMessage.SEVERITY_ERROR,"Invalid Name", "Invalid Name"));
}
}
But they do not show up and the server log keeps printing the following warning:
There are some unhandled FacesMessages, this means not every
FacesMessage had a chance to be rendered
How do I show them in the page?
And, a second question, if I submit the form with three empty input fields, it shows "required input error message" in each <p:message> associated with the <p:inputText>. How do I show them in the <p:message globalOnly="true">?
How do I show them in the page?
You need to specify a valid client ID. The client ID is not the same as component ID. The client ID is whatever you see in the JSF-generated HTML output. Also, the client ID should be the one of the input component, not of the message component.
So, given a
<h:form id="particular">
<p:inputText id="name1" />
<p:message id="msg1" for="name1" />
</h:form>
the input component has the client ID particular:name1 (rightclick page in browser and do View Source to see it yourself). So, the message should be attached on exactly this client ID.
context.addMessage("particular:name1", message);
And, a second question, if I submit the form with three empty input fields, it shows "required input error message" in each <p:message> associated with the <p:inputText>. How do I show them in the <p:message globalOnly="true">?
The <p:message> is not the valid component for that, you should use <p:messages>.
<p:messages id="msg" globalOnly="true" display="text"/>
The globalOnly="true" attribute means that only messages with a null client ID will be shown. So, you should just add exactly such a message.
context.addMessage(null, message);
See also:
How to use JSF h:messages better?
When and how is clientID generated in JSF?
Unrelated to the concrete problem, the non-global messages ("Invalid name") which you're trying to add there should actually be done by a fullworthy validator. See also How to perform validation in JSF, how to create a custom validator in JSF for a concrete example.

JSF Required Field only call message if i leave them

I have a problem with the JSF validation. I have the following form - only the first inputfield is required. So if I leave them empty and click on the primefaces calendar field - JSF render the whole form and of course I got a message that the field is required.
So I need a solutions that the field only call the blur event or required message when I leave the field not when I click another field etc.
<h:form>
<p:growl id="msg" showDetail="true" />
<b:panelGrid col-spans="4,8" columns="2" id="inputPanel">
<h:outputLabel value="ID*"></h:outputLabel>
<b:inputText value="#{managedBean.avsId}" required="true"
update="msg" onblur="ajax:managedBean.checkDublicates()"></b:inputText>
<h:outputLabel value="Legal Form"></h:outputLabel>
<b:switch animate="true" on-text="natural" offColor="danger"
onColor="success" off-text="forensic"
value="#{managedBean.legalform}">
</b:switch>
<h:outputLabel value="Date"></h:outputLabel>
<p:calendar value="#{managedBean.date}" pattern="dd.MM.yyyy">
<f:converter converterId="timestampConverter" />
</p:calendar>
<h:outputLabel value="Note" style="margin-top:5%;"></h:outputLabel>
<b:inputTextarea style="margin-top:5%;"
value="#{managedBean.note}"></b:inputTextarea>
</b:panelGrid>
<b:commandButton value="apply" look="success"
style="width:100%;" action="#{managedBean.create()}"
update="#form" />
</h:form>

primefaces fileupload required attribute not working

I have a JSF form which contains a Primefaces fileupload component.
The required attribute of the fileupload component is not working; When I hit the form's send button without file attachments, the form is sent and no error message is displayed.
This is the simplified form code I am using for testing:
<h:form id="NewRegistryForm">
<fieldset>
<p:outputLabel for="address" value="Address" />
<h:inputText
name="address"
value="#{registryBean.address}"
required="true"
requiredMessage="Address field is required" />
</fieldset>
<fieldset>
<p>File attachments:</p>
<p:fileUpload
fileUploadListener="#{registryBean.handleFileUpload}"
mode="advanced"
label="Choose file"
multiple="true"
update="registryFileList"
invalidFileMessage="Invalid file type"
sizeLimit="512000"
fileLimit="15"
allowTypes="/(\.|\/)(jpg|png|gif|pdf|odt|doc|docx)$/i"
onstart="setSubmitButtonEnabled(false)"
oncomplete="setSubmitButtonEnabled(true)"
onerror="setSubmitButtonEnabled(true)"
auto="true"
required="true"
requiredMessage="A file attachment is required." />
</fieldset>
<p:dataList id="registryFileList" value="#{registryBean.uploadedFiles}"
var="file" emptyMessage="" >#{file.file.fileName}, #{file.file.size}</p:dataList>
<p:commandButton
type="submit"
action="#{registryBean.sendRequest}"
value="Send form"
validateClient="true" />
</h:form>
The required text input field shows an error message when the field is not filled, but the fileupload component produces no message when no file is attached. What am I doing wrong?

immediate="true" on both input component and command component doesn't trigger required="true" on input component

My sample form is given blow. When I click on the "Login" button, validation message is shown for the userName field as immediate="true".
And when I click on the "Forgot Password" button, page is directed to my success page. As immediate=true present for button and userName input field, I am expecting required field validation on user name. But this is not the case.
Am I missing anything in this code?
<h:form>
<h:panelGrid columns="3">
<h:outputText value="User Name" for="userName" />
<h:inputText id="userName" value="#{myBean.userName}" required="true"
requiredMessage="User Name is required" immediate="true"
binding="#{myBean.userNameText}"></h:inputText>
<h:message for="userName" />
<h:outputText value="User Password" for="userPwd" />
<h:inputText id="userPwd" value="#{myBean.userPwd}" required="true"
requiredMessage="User Password is required"
binding="#{myBean.userPwdText}">
</h:inputText>
<h:message for="userPwd" />
<h:commandButton value="Login" action="#{myBean.action()}"></h:commandButton>
<h:commandButton value="Forgot Password" action="#{myBean.action()}"
immediate="true"></h:commandButton>
</h:panelGrid>
</h:form>

richfaces and displaying errormessages

<h:form id="userSettingsForm">
<rich:messages>
<f:facet name="errorMarker">
<h:graphicImage value="/img/msgerror.png" />
</f:facet>
</rich:messages>
<h:outputText value="Description:" />
<h:inputText label="Description:" id="description"
value="#{userSettingsForm.instance.description}" required="true"
size="5">
<f:validateLength minimum="3" />
</h:inputText>
<a:commandButton value="Validate" />
</h:form>
When an error occurs I see e.g. this: screenshot
How can I display the text "Description" in front of "Required field" ?
Like it is used on the richfaces demo page at richfaces demo page
Take a look at <rich:message> - it shows a single message.
I found out that I was missing something in my messages_en.properties file.
When I create one using seam setup the generated file contains e.g. this:
javax.faces.converter.IntegerConverter.INTEGER=Value is not a number
javax.faces.component.UIInput.REQUIRED=Required field
When I add:
javax.faces.converter.IntegerConverter.INTEGER={2}: Value is not a number
or
javax.faces.component.UIInput.REQUIRED={0}: Required field
The field label is added in front of the message.

Resources