Reputation: 2143
Lets say we have a form with some fields required and some not (optional).
Now when we say field is required then validator will only validate length of the
input but won't first trim the input and then validate.
To solve this problem i wrote a javascript function called on form submit. But it will also validate optional fields.
Adding a converter to all the input field is not elegant.
Also is their any way i can override required validation process .
Or how can i identify optional input field in javascript.
So what is the right way to do this.
using JSF 1.2
Upvotes: 0
Views: 1406
Reputation: 1109725
If you register the converter on <converter-for-class>
instead of <converter-id>
then it will be applied on all value types.
<converter>
<converter-for-class>java.lang.String</converter-for-class>
<converter-class>com.example.EmptyToNullConverter</converter-class>
</converter>
with
public class EmptyToNullConverter implements Converter {
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.trim().length() == 0) {
if (component instanceof EditableValueHolder) {
((EditableValueHolder) component).setSubmittedValue(null);
}
return null;
}
return value;
}
@Override
public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
return value == null ? null : value.toString();
}
}
No need to register it on every component.
Upvotes: 1