Search code examples
variablestypesaxaptadynamics-ax-2012x++

How to check the Year EDT lenght?


I created a Table's Field, and in addition to this, I created my custom EDT, named : MyEDT (for example).

MyEDT is INTEGER type and I have extended the System standard EDT YearBase. So, if I insert the alphabetic characters (look like "abecjskjfh") I get an error.

But I need to have a rule, I want to insert only value with 4 Number character, I only want values look like : 2000 , 2006, 1982 etc... .

I can check/control this by code, in methods validateWrite or validateField I insered this code :

switch (p1)
{
 case fieldNum(MyTable, MyField)  :   
           if (strLen( (strFmt("%1",this.MyField)) ) != 4)
           throw error ("Inser only value AAAA");
           break;
}

But,It's possible or exist to creato or axtends the YEAR EDT with only 4 number char length ? Or, there is another way to check the length the field valu ?

Thanks all,

enjoy!


Solution

  • If you want to use an integer EDT, I think there is no way to restrict the range of allowed numbers, except the AllowNegative property. So you have to do the validation in code like in your question. But I would suggest to change your validation logic to validate a number range instead of casting the number to a string and then validating the number of characters. This way you could also make sure that users cannot enter a year like 0000.

    if (this.MyField< 1900 || this.MyField > 9999)
    {
         throw error("Please enter a year between 1900 and 9999");
    }
    

    Another possibility could be to use a date EDT where you set the DateYear, DateMonth and DateDay properties such that only the year is shown. This would also help with data entry (e.g. 2 gets replaced with 2002) and gives you a nice error dialog if the users enters for example "abc".

    Screenshot of example using date EDT that only shows the year