views:

549

answers:

1

I've found several posts and articles around the net talking about validating form fields in dialogs, but none of the examples I've found seem to work properly.

Can someone post a complete, concise example of x++ code that generates a dialog containing a single text field, performs simple validation (if text = "abc") on it, and either closes the window (returning the field value) if validation passes or generates an Infolog warning without closing the dialog if validation fails.

For those of us just beginning in x++, I think it would be a great starting point to have an actual working example to build on.

Thanks!

+1  A: 

Here is an example in AX 2009 of how to build a simple dialog using the RunBase class. In it I create a class called DialogExample and derive from RunBase. To show the dialog you simply need to run the class, but typically this would be done by pointing a MenuItem at the class.

public class DialogExample extends RunBase
{
    DialogField dialogName;
    Name name;

    #DEFINE.CurrentVersion(1)
    #LOCALMACRO.CurrentList
        name
    #ENDMACRO
}

Object dialog()
{
    Dialog dialog = super();
    ;

    // Add a field for a name to the dialog. This will populate the field with 
    // any value that happens to be saved in name from previous uses of the
    // dialog.
    dialogName = dialog.addFieldValue(TypeId(Name), name);

    return dialog;
}

boolean getFromDialog()
{
    ;

    // Retrieve the current value from the dialog.
    name = dialogName.value();

    return true;
}

boolean validate(Object _calledFrom = null)
{
    boolean isValid;

    isValid = super(_calledFrom);


    // Perform any validation nessecary.
    if (name != 'abc')
    {
        isValid = checkFailed('Name is not equal to abc') && isValid;
    }

    return isValid;
}

Name parmName()
{
    ;

    return name;
}

public container pack()
{
    return [#CurrentVersion,#CurrentList];
}

public boolean unpack(container _packedClass)
{
    int version = conpeek(_packedClass, 1);

    switch (version)
    {
        case #CurrentVersion:
            [version,#CurrentList] = _packedClass;
            break;
        default :
            return false;
    }

    return true;
}

public static void main(Args args)
{
    DialogExample DialogExample;
    ;

    dialogExample = new dialogExample();

    // Display the dialog. This only returns true if the the user clicks "Ok" 
    // and validation passes.
    if (dialogExample.prompt())
    {
        // Perform any logic that needs to be run.
        info(dialogExample.parmName());
    }
}

Typically in this scenario logic that needs to be run would be put in a run method on the class and then called into from main if the Ok button is clicked. Since the run method would be an instance method this gets rid of the need for the parm methods to access the value of the field on the dialog.

dlannoye
Thanks for taking the time to post this excellent response. It works perfectly.
Brad