3

我在网上找到了几篇关于在对话框中验证表单字段的帖子和文章,但我发现的示例似乎都没有正常工作。

有人可以发布一个完整、简洁的 x++ 代码示例,该示例生成一个包含单个文本字段的对话框,对其执行简单验证(如果 text = "abc"),如果验证通过则关闭窗口(返回字段值)或如果验证失败,则在不关闭对话框的情况下生成信息日志警告。

对于我们这些刚开始使用 x++ 的人来说,我认为拥有一个可以构建的实际工作示例将是一个很好的起点。

谢谢!

4

2 回答 2

5

这是 AX 2009 中的一个示例,说明如何使用 RunBase 类构建一个简单的对话框。在其中,我创建了一个名为 DialogExample 的类并从 RunBase 派生。要显示对话框,您只需要运行该类,但通常这可以通过将 MenuItem 指向该类来完成。

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());
    }
}

通常在这种情况下,需要运行的逻辑将放在类的 run 方法中,然后如果单击 Ok 按钮,则从 main 调用。由于 run 方法将是一个实例方法,这消除了 parm 方法访问对话框上字段值的需要。

于 2009-11-20T23:57:06.690 回答
2

我知道这是一个老问题,但也应该注意的是,对于刚开始 AX 开发世界的人来说,AOT 中有很好的工作代码示例,请查找前缀为“Tutorial_”的表单和类。

Tutorial_RunBaseForm 是 AOT 中的一个类,它可以为您提供所需的内容。

于 2012-03-27T11:33:19.623 回答