Custom JSF f:validateLenght message without using #FacesValidator class - validation

In this example
<h:inputSecret value="#{contact.password}">
<f:validator validatorId="passwordValidator"/>
<f:validateLength minimum="8"/>
</h:inputSecret><br/>
If i put a password shorter than 8 letters i have j_idt10: Validation Error: Length is less than allowable minimum of '8' which is exactly what i want but i would like to rewrite the error message. I can do it using a custom #FacesValidator and i checked if there are some attribute in f:validateLength to do this but i didn't find any. Is writing a FacesValidator the only solution ?

Use message-bundle (similar but not same as resource-bundle) to override default conversion and validation messages:
https://stackoverflow.com/a/2668602/1072089

Related

Stripes Framework expression validation on property

I am trying to add expression validation on my property on actionbean but I am unable to make it work. I have even tried with integers like this >0 but still the same exception is thrown. Below is the code
#Validate(required=true, minvalue=1, expression="${this > maxBudget}")
int minBudget;
int maxBudget;
I am getting the below exception:
net.sourceforge.stripes.exception.StripesRuntimeException: Could not parse the EL expression being used to validate
field minBudget. This is not a transient error. Please double check the following expression for errors: ${this > maxBudget}
caused by
javax.el.ELException: The identifier [this] is not a valid Java identifier as required by section 1.19 of the EL specification (Identifier ::= Java language identifier).
This check can be disabled by setting the system property org.apache.el.parser.SKIP_IDENTIFIER_CHECK to true.
I have tried few variation, but every time it throws this exception.
Can some one please point out the mistake I am doing here
thanks
If you want to make sure minBudget is larger than maxBudget (isn't that the other way around?) you could just do:
#Validate(required=true, minvalue=1, expression="${minBudget > maxBudget}")
For greater flexibility you could consider implementing a custom validation method:
#ValidationMethod(on={"show"})
public void checkBudgetRange(ValidationErrors errors) {
if (minBudget < maxBudget)
errors.addGlobalError( new SimpleError("This is not good..."));
// all sorts of other checks to your liking
}
The on array argument holds the name(s) of the event handler(s) for which you want to perform this validation method. So in the example here that would be public Resolution show().
There's an excellent explanation at the Stripes Framework site at https://stripesframework.atlassian.net/wiki/display/STRIPES/Validation+Reference
UPDATE:
If you want to make use of the this keyword in validation expressions you may need to add a VM argument to your server (tested this on Tomcat 8):
-Dorg.apache.el.parser.SKIP_IDENTIFIER_CHECK=true
Otherwise the abovementioned error may be thrown.
The default value of org.apache.el.parser.SKIP_IDENTIFIER_CHECK was changed from true to false as of version 7 in Tomcat.
https://tomcat.apache.org/tomcat-6.0-doc/config/systemprops.html
https://tomcat.apache.org/tomcat-7.0-doc/config/systemprops.html

Angular Formly : required validation doesn't work if used as model[field] instead of model.field

below is the failure jsbin, where conditional required validation is not working
https://jsbin.com/xenuzeripa/1/edit?js,output
if i remove the dot's form the field name and change them to as below, i can use these fields as model.field instead of model[field] and everything starts working
Employment_Information__c.Other_Income_Picklist__c --> Other_Income_Picklist__c
Employment_Information__c.Annual_Income__c --> Annual_Income__c
not sure what's the issue
The expression has to be valid. It needs to appear as: model['field'] (with the field as a string). Here's an updated version: https://jsbin.com/juharo/edit?js (the double !! is to make sure that the required attribute gets assigned a boolean, rather than the value of model['field']).

Implicit expression language object "component" not working in jsf 2.2.6

I'm migrating a jsf 2.0 application to jsf 2.2.6. There is a extensive use of implicit EL object component as styleClass="#{component.valid?'':'err'}".
In jsf 2.2.6 (jsf-impl-2.2.6-jbossorg-4.jar) valid is not recognized, throwing "ServletException: The class 'javax.faces.component.html.xxx' does not have the property 'valid".
Is this functionality deprecated in jsf 2.x.x?
Can be related to JBoss EL?
It seems that you trying the component.valid on element that does not support it at all, for example the h:panelGroup does not have the isValid getter , while h:inputText does.
A workaround could be to abuse the validation status of another element in your page in order to apply the styleClass of another, see example:
<h:panelGroup styleClass="#{myComponent.valid ? '' : 'error'}">
<h:inputText id="input" value="#{myBean.myValue}" binding="#{myComponent}">
</h:inputText>
</h:panelGroup >
Finally found the reason of the exception. The problem was that I had a comment in the code containing "component.valid". Removing the comment resolves the problem.
<!-- styleClass="#{component.valid ? '': 'err' }" -->
It's tricky. The exception was not clear about the line of code.

How to override OmniFaces default validation/conversion error messages?

I'm using some OminFaces (1.8.1) validators as for example,
<o:validateAllOrNone components="a b c d" showMessageFor="someComponent"/>
If at least one of the specified fields is left blank in which case it displays a default message like as shown below.
a, b, c, d: Please fill out all or none of those fields
I want to override such error messages in resource bundles especially to get a localized message.
Unlike JSF, no resource bundles are found in OmniFaces. Is this still possible to override this error message somehow?
You can use the message attribute for that.
<o:validateAllOrNone components="a b c d" showMessageFor="someComponent"
message="#{i18n['some.bundle.key']}" />
where i18n is the <resource-bundle><var> of your resource bundle.
Indeed, the OmniFaces ValidateMultipleFields components do not support providing those messages via <message-bundle> without the need to declare the message attribute everytime.
Coincidentally, 3 days ago I've for the upcoming OmniFaces 2.0 committed several changes in those validators which should make it possible to override the default message via <message-bundle> when using the component type as key. So in case of <o:validateAllOrNone> which has a component type of "org.omnifaces.component.validator.ValidateAllOrNone", you should be able to override it in the resource bundle as identified by <message-bundle> as follows:
org.omnifaces.component.validator.ValidateAllOrNone = {0} all or none!

What is the proper way to format an error message in Spring?

I'm using Spring 3.1.0.RELEASE. How do you properly format an error message in Spring? In my properties file, I have …
Incomplete.headers.fileData=The file is missing the following required header(s): %s
Then in my validation class, I have
public void validate(Object target, Errors errors) {
...
final String missingHeadersStr = Joiner.on(",").join(missingHeaders);
errors.rejectValue("fileData", "Incomplete.headers.fileData", new Object[] {missingHeadersStr}, "The file is missing some required headers.");
However, even though (through debugging) I've verified that the "missingHeadersStr" field is non-empty, all that displays on my JSP is
The file is missing the following required header(s): %s
What is the proper way to format an error message in Spring? For what its worth, I'm displaying them on the JSP like so
<form:errors path="fileData" cssClass="error" />
The documentation says:
errorArgs - error arguments, for argument binding via MessageFormat
(can be null)
(emphasis mine).
You're giving a pattern usable with String.format(). But MessageFormat doesn't use that. The patter should be:
The file is missing the following required header(s): {0}

Resources