Masking a Field

From ADempiere
Jump to: navigation, search
This Wiki is read-only for reference purposes to avoid broken links.

Tutorial to mask a field in ADempiere

Masks that can be applied to ADempiere

The field length is the same as the length that's used for the mask. For example, if your mask is like 00-000/000 your total length will be equals to 10, but you'll only be able to input 8 characters into it, as the ponctuations will be automatically inputed. The control characters that can be used are as follows:

(Space) any character
_	Space (fixed character)
l	any Letter a..Z NO space
L	any Letter a..Z NO space converted to upper case
o	any Letter a..Z or space
O	any Letter a..Z or space converted to upper case
0	Digits 0..9 NO space
9   Digits 0..9 or space
a	any Letters & Digits NO space
A	any Letters & Digits NO space converted to upper case
c	any Letters & Digits or space
C	any Letters & Digits or space converted to upper case
Z	any characters converted to upper case

Adding a static Mask to a field

To mask a field in ADempiere in a static way, you should login into ADempiere as System, then go to Menu > Application Dictionaty > Table and Column and choose the table Test for our example.
In the Test table, choose the column Description, set the reference to String (It should be string, but just in case) and then set the mask you want in Value Format. In our example, the Value Format will be:


Now you can test it, still as system under Menu > System Admin > General Rules > Test fill the Description field.
Now you type any numbers in there, your format should be applied. Note that you'll not be able to input any other characters then Numbers, as the control characters explain (0 Digits 0..9 NO space)

Adding a Mask to a field at Runtime

Now for the real deal, as what has been showed so far is just common functionality of ADempiere. For some projects, like Localization-Brazil, there's a need to change the mask being used by the field at runtime, so the field can be used for two different information, making it easier to mantain some fields, for example, the federal inscription that everyone gotta have in Brazil, if you're a company, you need to have a CNPJ, if you're a person, you need to have a CPF. The functionality to handle this, works in the following way:

To set the new mask for the VFormat field:
Modified the following classes, and added the following methods:
GridTab - setFieldVFormat (String identifier, String strNewFormat)
GridTable - setFieldVFormat (String identifier, String strNewFormat)
GridField - setVFormat(String strNewFormat)

To load the new VFormat field I had to change one of the org.compiere.grid
and one of the org.compiere.grid.ed classes:
The following classes were modified to accomplish this:
GridController - Added the following lines to the dynamicDisplay method, right after the check context and visibility tests:
if (comp instanceof VString){ 
	VString ve = (VString)comp;
	if(!ve.getVFormat().equals(mField.getVFormat()) && mField.getVFormat() != null){

VString - setVFormat (String strMask)

Well, now to add a default mask for the field you could just do it as it's done in the example I posted above in the Masks that can be applied to ADempiere topic. Then you need to create a callout to set the mask to a default value, or use the one that's already existent for the T_Integer field in this table. The callout code should be like:

public String test(Properties ctx, int windowNo, GridTab mTab,
		GridField mField, Object value) {
	if (isCalloutActive())
		return "";
	String strFiscalInscriptionType = mTab.get_ValueAsString("T_Integer");
	String strMask = null;
	if (strFiscalInscriptionType.equals("0"))
		strMask = new String("00.000.000/0000-00");
		strMask = new String("000.000.000-00");
	mTab.setFieldVFormat("Description", strMask);
	return "";

Now, if you go into the Integer field and set the value to 1, the mask should be 000.000.000-00, if you set the value to 0, the mask should have changed to 00.000.000/0000-00.


This simple patch is usefull for a lot of other fields, not just for CNPJ or CPF.
There's no need to have a prior VFormat set for the column.. It can be added in runtime also.
If there's any need for help, feel free to contact me (fer_luck).
If you want to see the tracker that originated this model, take a look at