Home / Questions / validate() - Form data source field methods in D365 F&O - X++ Code
Explanatory Question

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

👁 977 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

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