Validation message of an inputFile is thrown in the console [duplicate] - validation

I created an user interface with a form to get information from user. When I submit the form, it prints the following warning in the server log:
INFO: WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
sourceId=j_idt7:j_idt11[severity=(ERROR 2), summary=(j_idt7:j_idt11: Validation Error: Value is not valid), detail=(j_idt7:j_idt11: Validation Error: Value is not valid)]
I tried to solve it, however I didn't understand how. How is this problem caused and how can I solve it?

As to the warning referring an undisplayed Validation Error: Value is not valid message, this means that you've somewhere a <h:selectXxx> component such as <h:selectOneMenu> without an associated <h:message> and therefore JSF is not able to display faces messages about conversion/validation errors directly in the UI.
In order to fix the particular warning, just add a <h:message>:
<h:selectOneMenu id="foo" ... />
<h:message for="foo" />
Note that when you're ajax-submitting the form using <f:ajax>, then you should not forgot to include the messages in the ajax-update. To start with, use render="#form" to update the entire form.
As to the concrete validation error problem which is mentioned in the message, head to the following answer: Validation Error: Value is not valid.

If you are using primefaces, dont forget to add:
<h:form>
<p:growl id="messages" showDetail="true" />
....
</h:form>

It is invalid value problem. Most probably you entered character instead of integer value. Check this and I suggest exception handling to not face these type of problems again.

Related

individual error message for each jsf validator tag

I have an input field which must meet the following restrictions: the input data should have exactly 2 characters, it should accept only letters and numbers, and it must be uppercase. So far, i did the following:
<h:inputText id="code" >
<f:validateLength minimum="2" maximum="2" />
<f:validateRegex pattern="^[a-zA-Z0-9]*$"/>
<f:validateRegex pattern="^[A-Z0-9]*$"/>
</h:inputText>
However, i need individual error messages when any of these validations fails. I did some search, and i found the following UNsuitable solutions:
1) How to customize JSF validation error message : Providing an validatorMessage attribute inside the text tag won't do the job, because this way i can provide only one message per input text tag.
2) http://incepttechnologies.blogspot.ro/p/validation-in-jsf.html : Validation method in the backing bean, or imperative validation using #FacesValidator annotation, are not good because this is exactly what i try to avoid; i want to move validation from back-end to front-end
3) https://www.mkyong.com/jsf2/customize-validation-error-message-in-jsf-2-0/ : overriding the error message in messages.properties is not good for two reasons: 1. I want to use custom error messages only locally (page scope), not for the entire application. 2. I have the same validator tag occuring twice - but with different patterns, and i want a different error message for each situation
The documentation for the validation tags (http://docs.oracle.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/ and http://docs.oracle.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/f/validateLength.html) specifies no attribute such as 'message', or 'errorMessage', as I hoped.
I have not tFied Kt but I think a woraround will be to place the custom overridden validation messages in a locale specific (which can be used only for this case ) properties file and use locale attribute of f:view tag to set the locale at the page level so that the messages are displayed from the locale specific message bundle.
A temporary solution is to concatenate all error messages into one big message. In case any validation fails, the corresponding message is anyway included in the big message.
So i made use of the validatorMessage attribute in the inputText tag.
Also, in order not to get the large message multiple times (when more than one of the validations fails) i replaced the 3 validations with an equivalent one:
<f:validateRegex pattern="^[A-Z0-9]{2}$"/>

Distinguish between conversion failure and validation failure in o:viewParamValidationFailed

I am using the OmniFaces <o:viewParam> taghandler in my current project. I like it, it's great. And even greater is the <o:viewParamValidationFailed>. Now, we are able to send an error if validation or conversion fails. But I wonder, whether it is possible to distinguish between conversion failure and validation failure.
Let's say we want to send a Bad Request if the given view param in malformed and can not be converted; for that matter send a Not Found if conversion succeeded, but the object could not be found in the database; and send a Forbidden if the successfully fetched object should not be accessed by the user.
Does anybody know a way to achieve this?
It's unfortunately not possible to distinguish between a ConverterException and ValidatorException when you've only UIInput#isValid() at hands. Theoretically, you could check and test the faces message to see if it represents a conversion or validation error, but this is not a robust approach, certainly not when it's localized.
On the other hand, it's possible to declare multiple view parameters on the same parameter name. You do not necessarily need to specify a value to set it as model value.
Here's an example based on your description, note that the model value is only set on the last one:
<o:viewParam name="foo">
<f:converter converterId="yourFooConverter" />
<o:viewParamValidationFailed sendError="400" />
</o:viewParam>
<o:viewParam name="foo">
<f:converter converterId="yourFooConverter" />
<f:validateRequired />
<o:viewParamValidationFailed sendError="404" />
</o:viewParam>
<o:viewParam name="foo" value="#{bean.foo}">
<f:converter converterId="yourFooConverter" />
<f:validateRequired />
<f:validator validatorId="yourRestrictedAccessValidator" />
<o:viewParamValidationFailed sendError="403" />
</o:viewParam>
To avoid the expensive job of calling the DB on every conversion, let the YourFooConverter implementation store the converted value as a custom attribute of the FacesContext and then check it on every pass.

OmniFaces validateOrder disabling

I'm trying to use validateOrder component to validate two java.util.Date objects. It is similar to showcase example on this link (PrimeFaces example). Everything works perfect, but i have one question:
What if 2nd date field is not required?
In that case i'm getting nullpointer exception, and since validateOrder has "disabled" attribute, i was wondering is it worth/possible enabling/disabling it via ajax every time the 2nd date is inserted/removed. If not, i guess i'll stick to Balus' approach for JSF2.0 cross-field validation that you can read about on this link.
Let the disabled attribute check if the 2nd field is filled in. If it's not filled in, the request parameter value associated with field's client ID will be empty. Use exaclty that to let disabled attribute evaluate to true.
<p:calendar ... binding="#{endDate}" />
...
<o:validateOrder ... disabled="#{empty param[endDate.clientId]}" />
Code is as-is. No additional backing bean property necessary for binding.
See also:
How does the 'binding' attribute work in JSF? When and how should it be used?

JSF: remove client id (label, component id) from validation message without a custom message

I have the following code:
<h:inputText id="it-date" value="#{myBean.myDate}">
<f:convertDateTime pattern="MM/dd/yyyy"/>
</h:inputText>
<h:message for="it-date" />
When I enter the date 02/30/2012, I get the following message:
j_idt5:it-date: '02/30/2012' could not be understood as a date. Example: 10/01/2012
Is it possible to remove j_idt5:it-date: including the colon and to keep the default message from Java?
I already tried to use the attribute label, but with that the colon remained. I don't want to use a custom message, because the default messages are already internationalized.
I guess you will need to get in between the message handling somehow to remove the colon because it is a fixed part of the message.
Checkout the message properties file, e.g. here:
http://grepcode.com/file/repo1.maven.org/maven2/com.sun.faces/jsf-api/2.1.7/javax/faces/Messages.properties
The specific message/property in this case would be:
javax.faces.converter.DateTimeConverter.DATE={2}: ''{0}'' could not be understood as a date.
Check out the fixed colon after the {2} . If you want to get rid of it but keep the actual message you would need some way to get in between the message handling.
Mabye this answer from BalusC is an option:
FacesMessage listener

Primefaces p:calendar with p:ajax value not updated (one step delay)

I want to call a bean after a date is selected in a p:calendar. In my setup I have a #Named #ViewScoped Bean, the class ObWithDate is a #Entity with a date field validFrom.
<h:form id="fUser">
<p:dataTable id="dt" var="cum" value="#{myBean.listObWithDates}">
<p:column>
<p:calendar id="cValidFrom" value="#{cum.validFrom}">
<p:ajax event="dateSelect" listener="#{myBean.update(cum)}"
update=":fUser:dt"/>
</p:calendar>
</p:column>
</p:dataTable>
</h:form>
The good thing is, that the bean method update(ObWithDate myO) is called with the correct object each time I select a date. The bad thing is, that myO.validFrom does not have the correct value. It's always one step behind:
Field initialized with 2012-01-01
GUI change to 2012-01-05, bean method debugs to 2012-01-01
GUI change to 2012-01-15, bean method debugs to 2012-01-05
This issue drives me crazy, I've found several issues with not correctly updated values and also some about p:calendar. Most of them suggest using the attribute selectListener="#{calendarBean.handleDateSelect}" with a handleDateSelect(DateSelectEvent event) method. But I want to call my method since I want to pass the current instance of the variable.
Firebug verifies, that the correct updated values are transmitted in a POST request
I'm using Primefaces 3.2 and JBoss-7.1.1-Final
Even without the p:dataTable and only using p:calendar the issue is still the same.
The wrong value of the debug output can be observed for the method-Object update(ObWithDate myO) as well for the corresponding entry in myBean.listObWithDates.
<p:ajax immediate="true" .../> does not solve this issue.
Update MattHandy helped me a lot, the resulting question How to get the corresponding list item in the ajax call inside the beans method? is formulated in the followup JSF p:calendar in p:dataTable: How to get the row of p:ajax dateSelect event.
This is most likely a lifecycle issue. The setter for your field is called before your listener executes. You should not use the listener to set your value.
The el expression on your listener is evaluated at render response phase of the previous request thus it holds the cum value "one step behind".
You should update your custom object from the setter of the calendar's date value.
I ran into this issue today morning. Found that primefaces have removed the DateSelectEvent from Primefaces 3.5 . Hence had to remove the ajax event call from my calendar object. Instead retrieving the value using get..(). Please find the discussion for the same in this link below.
http://forum.primefaces.org/viewtopic.php?f=3&t=27590

Resources