validate() - Form data source field methods in D365 F&O - X++ Code

Views 5

Answer:

The validate() method is a standard method on the field of the form data source. This method is called when a value is entered in a specific field, and it's overridden if extra field validation is needed.

Example

You have a custom form with a table named TrainingMaster. The table has a field named Participants, where entering a number higher than 20 isn't allowed.

The coding pattern can be as follows:


[DataSource]
class TrainingMaster
    {
        [DataField]
        class Participants 
        {
            public boolean validate()
            {
                boolean ret;
                ret = super() && (TrainingMaster.Participants <= 20);
                return ret;
            }
        }
    }

Make sure that you offer a reason for errors that are thrown, as shown in the following code example:


[DataSource]
class TrainingMaster
    {
        [DataField]
        class Participants 
        {
            public boolean validate()
            {
                boolean ret;
                if (TrainingMaster.Participants > 20)
                {
                    throw error("Maximum 20 participants");
                    return false;
                }
                ret = super();
                return ret;
            }
        }
    }

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of X++ Programming Language, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.