Hybrid View

  1. #1
    Ext GWT Premium Member icfantv's Avatar
    Join Date
    Sep 2011
    Location
    Superior, CO
    Posts
    411
    Vote Rating
    21
    Answers
    20
    icfantv will become famous soon enough icfantv will become famous soon enough

      0  

    Question Answered: Setting max length on TextField

    Answered: Setting max length on TextField


    Is there really no way to set the max length on a text field? I know I can use a max length validator but that doesn't prevent the user from entering more characters than is "allowed."

    There was a setMaxLength(...) in 2.x on the TextField object. Why was it pulled in favor of a validator?

  2. There is a way to set max length on a textfield.it passes the test to me.
    like this:

    public class MyTextField extends TextField {

    public MyTextField(){
    InputElement ie=this.getInputEl().cast();
    ie.setMaxLength(10);
    }


    }

  3. #2
    Sencha User PhiLho's Avatar
    Join Date
    Nov 2011
    Location
    Near Paris, France
    Posts
    140
    Vote Rating
    1
    Answers
    2
    PhiLho is on a distinguished road

      0  

    Default


    An obvious solution would be to set the maxlength attribute on the underlying input element, but I wasn't sure how to do it.
    I found a solution in a blog post:
    http://www.selikoff.net/2011/04/26/g...tfield-in-gxt/

  4. #3
    Ext GWT Premium Member icfantv's Avatar
    Join Date
    Sep 2011
    Location
    Superior, CO
    Posts
    411
    Vote Rating
    21
    Answers
    20
    icfantv will become famous soon enough icfantv will become famous soon enough

      0  

    Default


    Yes, that is an obvious solution indeed. :-) How to actually get to the DOM element is the tricky part.

    Thanks for that post. The solution I thought of (but was dreading) was something we had to do back in my Java Swing days which was to add a key up listener to the field and when the limit was hit, stop allowing input. But it's annoying because you still have to allow for the backspace and delete keys.

    I will use your posted solution. Thanks again.

  5. #4
    Ext GWT Premium Member icfantv's Avatar
    Join Date
    Sep 2011
    Location
    Superior, CO
    Posts
    411
    Vote Rating
    21
    Answers
    20
    icfantv will become famous soon enough icfantv will become famous soon enough

      0  

    Default


    Just getting back to this.

    In case anyone sees this in the future, here's what I've come up with - not sure if it's the "right" way, but it passes the smell test to me:

    public class BoundedTextField extends TextField {


    public BoundedTextField() {
    this(100);
    }


    public BoundedTextField(int maxLength) {


    super();
    if (maxLength <= 0) {
    throw new IllegalArgumentException("the max length must be positive: " + maxLength);
    }


    this.maxLength = maxLength;
    this.addValidator();
    }


    @Override
    public void onAfterFirstAttach() {


    XElement element = this.getInputEl();
    element.setAttribute("maxlength", Integer.toString(this.maxLength));
    }


    private void addValidator() {
    this.addValidator(new MaxLengthValidator(this.maxLength));
    }


    private final int maxLength;
    }

  6. #5
    Ext GWT Premium Member dardison's Avatar
    Join Date
    Apr 2008
    Location
    Buenos Aires, Argentina
    Posts
    174
    Vote Rating
    1
    dardison is on a distinguished road

      0  

    Default


    Hi,

    I would like to know what Sencha Team have to say about the issue.
    Anyone?

  7. #6
    Sencha Premium Member
    Join Date
    Jan 2012
    Posts
    8
    Vote Rating
    2
    SelzChi is on a distinguished road

      0  

    Default


    Hi,

    any news regarding this issue?

    Thanks,

    Selcuk