Along with couples inputText, one of a mandatory component that I have on the page is a p:fileUpload. So when I click submit, <p:message> show up on component that have require=true, but the user did not type/select
I want the red box Required also appear next to the upload component. Here is what I have tried.
1 . when I set required="true" in p:fileUpload, nothing really happen (not sure if this is a bug).
2 . I put validator in p:fileUpload, below is my validator sources
public void validateFileUpload(FacesContext context, UIComponent component,
Object value) throws ValidatorException {
if(value == null){
FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_ERROR);
message.setSummary("Error");
message.setDetail("Required");
throw new ValidatorException(message);
}
}
nothing really happen when I click submit, not even when I go through the upload, validateFileUpload did not get called at all (not sure if this is a bug)
3 . When I click submit, if everything else pass, and I get into my action method, I am able to check if the file is null or not, then return a FacesMessage and let p:growl pick it up. However, I dont like it that way since it give the user a feeling of multiple layer of validation.
Is there a way to do better validation on p:fileUpload?
For those with the same problem, I ran into this problem while creating a wizard. The workaround I used was to store the uploaded file in a field of my viewscoped bean and check this field when trying to navigate to the next step.
Wizard tag:
<p:wizard id="importBankAccountLogWizard"
widgetVar="importBankAccountLogWizard"
flowListener="#{bankAccountLogImportBean.onFlowProcess}">
File upload tag (I have the rendered and the update attribute set up so that a message will be shown and the uploaded will be hidden after the first upload):
<p:fileUpload id="bankAccountLogFileInput"
fileUploadListener="#{bankAccountLogImportBean.setBankAccountLogFile}"
rendered="#{bankAccountLogImportBean.renderFileUploadInput}"
mode="advanced"
update="importBankAccountLogWizard"
auto="true"
sizeLimit="1000000" />
Bean:
public void setBankAccountLogFile(FileUploadEvent event)
{
importFile = event.getFile();
FacesMessage msg = new FacesMessage(Localization.g("FILE_HAS_BEEN_UPLOADED", event.getFile().getFileName()));
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public String onFlowProcess(FlowEvent event)
{
if("bankAccountLogImportInputTab".equals(event.getOldStep()) &&
importFile == null)
{
FacesMessage msg = new FacesMessage(Localization.g("UPLOAD_A_FILE_TO_CONTINUE"));
FacesContext.getCurrentInstance().addMessage(null, msg);
return event.getOldStep();
}
return event.getNewStep();
}
Related
I have the following problem. I use a form when I provide only PIN. I have validator which checks if its a 4-digit number. Then the action on submit is set to the method which checks if the PIN exists in the database. If not it does message = "no PIN"; I used the message in the output label below the form. Previously it was null so there was no message there. Now it changes into "no PIN" but I have to clear it after clicking the submit button again because the error message doesn't disappear when you enter for example "12as" PIN and validator takes care of it. How should i implement such situation? Maybe using an output label in such situtation is a wrong idea?
You should not perform validation in action method. You should use a real validator.
Just implement the Validator interface accordingly. E.g.
#FacesValidator("pinValidator")
public class PinValidator implements Validator {
#Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
String pin = (String) value;
if (pin == null || pin.isEmpty()) {
return; // Let required="true" deal with it if necessary.
}
if (!pin.matches("\\d{4}")) {
throw new ValidatorException(new FacesMessage("PIN must be 4 digits"));
}
if (!somePinService.exists(pin)) {
throw new ValidatorException(new FacesMessage("PIN is unknown"));
}
}
}
Use it as follows:
<h:outputLabel for="pin" value="PIN" />
<h:inputText id="pin" value="#{bean.pin}" validator="pinValidator" />
<h:message for="pin" />
The faces message of the validator exception will end up in the <h:message> associated with the component on which the validator is been fired.
If you're using ajax to submit the form, don't forget to make sure that the message is also taken into account on ajax render.
Unrelated to the concrete problem, the JSF <h:outputLabel> generates a HTML <label> element which is intented to label a form element (e.g. <input>, <select>, etc). It's absolutely not intented to show an arbitrary piece of text such as a validation message. I recommend to put JSF aside for now and start learning basic HTML. This way you will understand better which JSF components to pick to get the desired HTML output.
You can use JSF message component outside a validator:
For a message for your input in your form:
<h:message for="PIN"/>
And at your managed bean you can add a FacesMessage using:
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN,"No pin summary message","No pin detail message");
FacesContext.getCurrentInstance().addMessage("PIN", message);
No need to use a outputLabel here.
I use a method like this for sending error messages to my facelet:
public static void addErrorMessage(String msg) {
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
FacesContext.getCurrentInstance().addMessage(null, facesMsg);
}
Then I show them inside a dialog with:
<p:messages id="messagesFattura" autoUpdate="true" />
And that works well. Now, my question is:
how can I "check" for messages existence?
I mean, there's another portion of code (a primefaces ) which I want to show/hide conditionally, via the rendered attribute.
I already tried the #{empty facesContext.messageList} EL variable, without success (empty list)
I have three radio buttons, an inputText and a submit button. I only want to validate the input text on submit when a certain radio is selected. So I have
<h:inputText validator="#{myBean.validateNumber}" ... />
And in my bean I have
public void validateNumber(FacesContext context, UIComponent component,
Object value) throws ValidatorException{
if(selectedRadio.equals("Some Value"){
validate(selectedText);
}
}
public void validate(String number){
if (number != null && !number.isEmpty()) {
try {
Integer.parseInt(number);
} catch (NumberFormatException ex) {
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Error", "Not a number."));
}
} else {
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Error", "Value is required."));
}
}
The one thing that make this not work is that when I submit, validateNumber(...) runs before my setter method for the radio button setSelectedRadio(String selectedRadio). Therefore causing this statements
if(selectedRadio.equals("Some Value"){
validate(selectedText);
}
to not execute correctly. Any idea on how to get around this problem?
The selectedRadio is as being a model value only updated during update model values phase, which is after the validations phase. That's why it's still the initial model value while you're trying to examine it.
You'd have to grab it from either the request parameter map (which is the raw submitted value), or the UIInput reference, so that you can get either the submitted value by getSubmittedValue() or the converted/validated value by getValue().
So,
String selectedRadio = externalContext.getRequestParameterMap().get("formId:radioId");
or
UIInput radio = (UIInput) viewRoot.findComponent("formId:radioId"); // Could if necessary be passed as component attribute.
String submittedValue = radio.getSubmittedValue(); // Only if radio component is positioned after input text, otherwise it's null if successfully converted/validated.
// or
String convertedAndValidatedValue = radio.getValue(); // Only if radio component is positioned before input text, otherwise it's the initial model value.
It is called cross-field validation (validate not only based in the value of a component, but a set of them).
Currently, JSF2 does not support it ( JSF doesn't support cross-field validation, is there a workaround? ) but there are several libraries (in the refered question omnifaces is mentioned, it looks like seamfaces also has something for it) that might help. Also in the question there is a workaround.
I'm trying to build a user profile page to show some details about my users.
The url of the page is something like profile.xhtml?username=randomString.
So, what I've to do is loading all the data of randomString's user.
It goes everything fine since it's the moment to show user's image.
I'm using PrimeFaces with graphicImage component, but the problem is that it causes a NEW request to get the image, so the request parameter is actually lost and the getAvatar() method receives a null parameter.
One solution may be making the bean SessionScoped, but it will take data from the first requested user and it will show them even if randomString will change, so I'm asking for help :
How can I show a dynamic image from database that depends on a request parameter?
Thanks :)
EDIT : New code following BalusC's reply
JSF Page :
<c:set value="#{request.getParameter('user')}" var="requestedUser"/>
<c:set value="#{(requestedUser==null) ? loginBean.utente : userDataBean.findUtente(request.getParameter('user'))}" var="utente"/>
<c:set value="#{utente.equals(loginBean.utente)}" var="isMyProfile"/>
<pou:graphicImage value="#{userDataBean.avatar}">
<f:param name="username" value="#{utente.username}"/>
</pou:graphicImage>
(I'm using this vars because I want the logged user's profile to be shown if page request il just profile.xhtml without parameters)
Managed Bean :
#ManagedBean
#ApplicationScoped
public class UserDataBean {
#EJB
private UserManagerLocal userManager;
/**
* Creates a new instance of UserDataBean
*/
public UserDataBean() {
}
public Utente findUtente(String username) {
return userManager.getUtente(username);
}
public StreamedContent getAvatar(){
String username = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("username");
System.out.println(username==null);
Utente u = findUtente(username);
return new DefaultStreamedContent(new ByteArrayInputStream(u.getFoto()));
}
}
What's wrong with it?
username is always null!
EDIT 2 : Added reply to BalusC
Yeah, because the getAvatar() method calls findUser() as I need to find the user's entity with the username passed as parameter (<f:param> won't allow me to pass an object!).
So findUser() throws an exception because I'm using entityManager.find() with a null primary key!
Btw, I'm absolutely sure that both #{utente} and #{utente.username} are not null because the panel that contains the image is rendered only if #{utente ne null} and username is its primary key!
So I can't really check the HTML output!
I'm afraid that #{utente} is lost when I call getAvatar() as getting an Image requires a new http request
Pass it as <f:param>. It will be added during render response.
<p:graphicImage value="#{images.image}">
<f:param name="id" value="#{someBean.imageId}" />
</p:graphicImage>
The #{images} helper bean can just look like this:
#ManagedBean
#ApplicationScoped
public class Images {
#EJB
private ImageService service;
public StreamedContent getImage() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getRenderResponse()) {
// So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
return new DefaultStreamedContent();
}
else {
// So, browser is requesting the image. Get ID value from actual request param.
String id = context.getExternalContext().getRequestParameterMap().get("id");
Image image = service.find(Long.valueOf(id));
return new DefaultStreamedContent(new ByteArrayInputStream(image.getBytes()));
}
}
}
As the above helper bean has no request based state, it can safely be application scoped.
i am using a4j for checking username exists or not for onblur event
it displays the error messages when user already exists but ,if i click on
submit button after displaying the error message it gets submitted
when it comes to the required=true for inputtext it doesn't get submitted
required="true" validator="#{RegistrationBean.checkFirstName}">
public void checkFirstName(FacesContext facesContext, UIComponent component, Object value)
{
String name = (String) value;
if(name.trim().length()==0){
System.out.println("Name *******");
String message = bundle.getString("Name_Required");
FacesContext.getCurrentInstance().addMessage("Reg:firstName",
new FacesMessage(FacesMessage.SEVERITY_ERROR, message,message));
}
if(name.trim().equalsIgnoreCase("tom")){
String message = bundle.getString("Name_exists");
FacesContext.getCurrentInstance().addMessage("Reg:firstName",
new FacesMessage(FacesMessage.SEVERITY_ERROR, message,message));
}
}
could anyone suggest me where i went wrong
With the JSF code, it would be easier to help you. So I suggest that you have something like that in your JSF page:
<h:inputText id="firstName" ... validator="#{aBean.checkFirstName}">
The problem on your Java code is that you do not throw any ValidatorException when an error occurs. Thus, your code must be:
public void checkFirstName(FacesContext facesContext, UIComponent component, Object value) {
String name = (String) value;
if ((name == null) || name.trim().length() == 0) {
System.out.println("Name *******");
String message = bundle.getString("Name_Required");
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message)); // Throw the required error
}
if (name.trim().equalsIgnoreCase("tom")) {
String message = bundle.getString("Name_exists");
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message)); // Throw the already exist error
}
}
This way, if the value filled in this field is empty or already exists, a ValidatorException will be thrown, the user will get an error message (do not forget to add the <h:messages/> or <h:message/> component in your form), and the form will not be submitted!