Suggestion for form

Hi all,

i can see that form class is at the very beginning, and only adds tab key support so i would like to suggest some improvement i see benefical to form

MyForm.java http://pastebin.com/REXqTm78

simple usage with two inputs, validation check and success/error events

[java]

    // Create window
    win = new Window(screen, new Vector2f(15f, 15f), new Vector2f(320f, 200f));
    win.setWindowIsMovable(false);
    win.setIsResizable(false);
    win.setWindowTitle("Add Server");
    win.showAsModal(false);
    win.centerToParent();
    screen.addElement(win);

    final MyForm form = new MyForm(screen) {

        @Override
        public boolean onValidate(HashMap<String, String> values) {
            boolean isValid = true;
            if(null != selectList) {
                 if(values.get("name").isEmpty()) {
                     isValid = false;
                 }
                 if(values.get("host").isEmpty()) {
                     isValid = false;
                 }
            }
            // Form is valid
            return isValid;
        }

        @Override
        public void onSuccess(HashMap<String, String> values) {
            if(null != selectList) {
                selectList.addListItem(values.get("name"), values.get("host"));
            }
            close();
        }

        @Override
        public void onError(HashMap<String, String> values) {
            DefaultAlertBox alert = new DefaultAlertBox(screen, Vector2f.ZERO);
            alert.setWindowTitle("Form validation");
            alert.setMsg("Server Name and Host are required.");
            screen.addElement(alert);
            alert.centerToParent();
        }
    };

    Label labelName = new Label(screen, new Vector2f(50f, 50f), new Vector2f(100f, 20f));
    labelName.setText("Server Name:");
    win.addChild(labelName);

    TextField inputName = new TextField(screen, new Vector2f(170f, 50f));
    win.addChild(inputName);
    form.addFormElement("name", inputName);
    inputName.setTabFocus();
    inputName.resetTabFocus();

    Label labelHost = new Label(screen, new Vector2f(50f, 80f), new Vector2f(100f, 20f));
    labelHost.setText("Server Host:");
    win.addChild(labelHost);

    TextField inputHost = new TextField(screen, new Vector2f(170f, 80f));
    win.addChild(inputHost);
    form.addFormElement("host", inputHost);

    // Add button
    ButtonAdapter addServer = new ButtonAdapter(screen, new Vector2f(50f, 150f)) {
        @Override
        public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
            form.submitForm();
        }
    };
    addServer.setText("Add Host");
    win.addChild(addServer);

    // Storno button
    ButtonAdapter storno = new ButtonAdapter(screen, new Vector2f(170f, 150f)) {
        @Override
        public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
            close();
        }
    };
    storno.setText("Storno");
    win.addChild(storno);

[/java]

its a little basic preview. if you are interested @t0neg0d i can improve it, so for example for every validation error, additional message can be added and so on… :slight_smile:

2 Likes

It’s a bit late tonight for me… but this is definitely working in the direction this was supposed to be going. But, I wanted to let you know I saw this… and would love to talk about the possibilities tomorrow if you have the chance!

yea no problem :slight_smile: its 10 o’clock here, and i have time from about 21-22 o’clock

edit: this is roughly how nette framework’s (php) form system works and im using it for some years now and it is very good, no hacks needed :slight_smile:

edit2: if you want to go more far, it also could have some basic validation rules, so for most time, you dont need to use onvalidation, because form itself can check fill, number, regular expression etc :slight_smile:

@Ascaria said: yea no problem :) its 10 o'clock here, and i have time from about 21-22 o'clock

edit: this is roughly how nette framework’s (php) form system works and im using it for some years now and it is very good, no hacks needed :slight_smile:

edit2: if you want to go more far, it also could have some basic validation rules, so for most time, you dont need to use onvalidation, because form itself can check fill, number, regular expression etc :slight_smile:

I have some fairly decent validation build into TextField that should probably have been part of form:

There are rules that are inforced on input:
DEFAULT,
ALPHA,
ALPHA_NOSPACE,
NUMERIC,
ALPHANUMERIC,
ALPHANUMERIC_NOSPACE,
EXCLUDE_SPECIAL,
EXCLUDE_CUSTOM,
INCLUDE_CUSTOM

And then there are field validations as well:
And… of course I can’t remember them off the top of my head, but they revolve around enforcing data types or some such thing.

Maybe these could be put into a usable format in form?

Here is the source for TextField.java … most of the validation is towards the top of the class

https://code.google.com/p/tonegodgui/source/browse/trunk/src/tonegod/gui/controls/text/TextField.java

i will look into it today’s evening :slight_smile:

1 Like