Thursday 17 April 2014

ADF - Convert inputText inputListOfValues to UpperCase

There are many articles about this topic.
Various techniques are proposed including: Javascript, converter, contentStyle etc..
I think that no solution is perfect.
For example:
Javascript: while typing the character changes.
Converter: the conversion happens only on the server side.
contentStyle: the conversion is only visual but the String submitted is not in uppercase.
For me, the best solution is to act in two places.
I consider excellent the use of the contentStyle to display uppercase letters:


In my case I use an inputText, but an inputListOfValues is equal to.
As I wrote before the contentStyle has an effect purely graphic but the value submitted is not converted.
We therefore need to combine the use of the contentStyle with the use of a Converter to convert the value also on server side.
So, we must implement a simple converter that implements the class: javax.faces.convert.Converter:


Very simple.
After that we must register this converter in faces-config.xml file:


Finaly in the converters list of our component we can choose the StringToUppercaseConverter:


In this way we not only see the uppercase letters when typing, but also the data will be submitted in uppercase.

But...Why this is not a perfect procedure?
In this way we remain uncovered on programmatic "setAttribute" calls.
For example if we call 
row.setAttribute("MyField","abc") 
for an attribute not in page, the converter (obviously not present) not will converts the value.

Attention:
I think that convert the data directly in the overridden setAttribute method  is not a good idea.
setAttribute should already be called with the right value.

No comments:

Post a Comment