0

Masked Input Component [closed]

asked 2013-05-04 18:45:54 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...

Hi

In my current project, i have created a small component to take care of inputs such as SSN, Zipcode, Phone , fax, etc.

Here is the code.

public class MaskBox extends Textbox {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private String format;


public String getFormat() {
    return format;
}

public void setFormat(String format) {
    this.format = format;
}


@SuppressWarnings({ "unchecked", "rawtypes" })
public MaskBox() {
    this.addEventListener(Events.ON_CREATE, new EventListener() {
        @Override
        public void onEvent(Event event) throws Exception {
            String mask = "jq(this).mask('" + format + "');";
            setWidgetListener("onBind", mask);

        }
    });
}

}

Sample Usage in zul file

<MaskBox format="99-9999999" id="ein" name="ein" hflex="1"                                          readonly="@load(vm.makeAsReadOnly)"                                          value="@bind(fx.ein) @converter('com.product.webapp.component.MaskConverter')" />

MaskConverter Code

public class MaskConverter implements Converter {

/**
 * The method coerceToUi() is invoked when loading ViewModel's property to
 * component and its return type should correspond to bound component
 * attribute's value[1]. The coerceToBean() is invoked when saving. If you
 * only need to one way conversion, you can leave unused method empty.
 */

public Object coerceToUi(Object val, Component comp, BindContext ctx) {
    // do nothing
    return val;
}

public Object coerceToBean(Object val, Component comp, BindContext ctx) {
    /*
     * Here we will check only masking characters are present, if so, then
     * return null
     */
    final String propValue = (String) val;
    if (IsEmptyByMask(propValue))
        return null;
    else
        return val;

}

public boolean IsEmptyByMask(String s1) {
    if (isEmpty(s1) == false) {
        s1 = s1.replaceAll("_", "").replace("(", "").replace(")", "")
                .replace("-", "").replace(" ", "").replace("/", "").trim();
        if (isEmpty(s1))
            return true;
        else
            return false;
    }
    return true;
}

public static boolean isEmpty(String s) {
    return s == null || s.trim().length() == 0;
}

}

Everything is working fine. Now i am just trying to remove the converter in the zul file and trying to identify the way of calling inside the MaskBox.java, so that in the zul file, i need no to tell explicitly.

I came to know you can override Widget Method in Java using this link.

Using that, we can override the setvalue method and remove the special characters. ?

delete flag offensive retag edit

The question has been closed for the following reason "the question is answered, right answer was accepted" by benbai
close date 2013-06-28 04:01:52

Comments

What do you want to achieve? e.g., Do you want to get the masked value or the value without mask at server side? Please describe it more details.

benbai ( 2013-05-05 04:22:09 +0800 )edit

Yes. you are right. Say for an example, if i do not enter any values, then in the db, it stores the mask characters. Thats why i written the converter. It is working fine. Since already i created as component, so i am trying to this conversion within the java itself without calling in zul

Senthilchettyin ( 2013-05-05 07:11:03 +0800 )edit

I guess the coerceToBean method in convetor is just called before onChange/onChanging events, if you just want to do the same thing without @converter, you can try override getValue or setValue methods or override the service method for onChange and/or onChanging events.

benbai ( 2013-05-05 08:05:16 +0800 )edit

Question tools

Follow
1 follower

RSS

Stats

Asked: 2013-05-04 18:45:54 +0800

Seen: 36 times

Last updated: May 04 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More