Question

Request you to please check validation code

Hi Sir/Madam,
I request you to please check, below validation code for data control(if age < 18, error message should be  displayed) I have added localized string given name(InvalidDOBFormat) and value(Incorrect) but error message is not displayed
    methods: {            
                this.addColumnValidator("UsrDateofbirth", this.dobValidator);            
     },
     dobValidator: function(value) {
                var invalidMessage = "";
                var isValid = true;
                var number = value || this.get("UsrDateofbirth");
                
                var nd = number.substr(0, 2);
                var nm = number.substr(3, 2);
                var ny = number.substr(6, 2);
                var cnumber = "";
                cnumber = nm + "-" + nd + "-" + ny;
                
                var today = new Date();
                var birthDate = new Date(cnumber);
                        
                //day
                var tdate = today.toString();
                var td = tdate.substr(8, 2);
                td = parseInt(td, td);
                var bd = number.substr(0, 2);
                bd = parseInt(bd, bd);
                var d = bd - td;
                        
                //year
                var age = today.getFullYear() - birthDate.getFullYear();
                
                //month
                var todayMonth = today.getMonth() + 1;
                var getMonthName =  birthDate.toString();
                var monthName = getMonthName.substr(4, 3);
                var bdmonthnumber;
                
                todayMonth = parseInt(todayMonth, todayMonth);
                
                if (monthName === "Jan") {
                    bdmonthnumber = 1;
                } else if (monthName === "Feb") {
                    bdmonthnumber = 2;
                } else if (monthName === "Mar") {
                    bdmonthnumber = 3;
                } else if (monthName === "Apr") {
                    bdmonthnumber = 4;
                } else if (monthName === "May") {
                    bdmonthnumber = 5;
                } else if (monthName === "Jun") {
                    bdmonthnumber = 6;
                } else if (monthName === "Jul") {
                    bdmonthnumber = 7;
                } else if (monthName === "Aug") {
                    bdmonthnumber = 8;
                } else if (monthName === "Sep") {
                    bdmonthnumber = 9;
                } else if (monthName === "Oct") {
                    bdmonthnumber = 10;
                } else if (monthName === "Nov") {
                    bdmonthnumber = 11;
                } else if (monthName === "Dec") {
                    bdmonthnumber = 12;
                }
        
                var m = todayMonth - bdmonthnumber;
                //console.log("m"+m);
                if ((age === 18) && (m === 0) && (d === 0)) {
                    isValid = true;
                } else if ((age === 18) && (m === 0) && (d < 0)) {
                    isValid = false;
                } else if ((age === 18) && (m === 0) && (d > 0)) {
                    isValid = true;
                } else if (age < 18) {
                    isValid = false;
                } else if (age > 18) {
                    isValid = true;
                }
                
                if (!isValid) {
                    invalidMessage = this.get("Resources.Strings.InvalidDOBFormat");
                }
                // Object which properties contain validation error messages.
                // If the validation is successful, empty strings are returned to the object.
                return {
                    invalidMessage: invalidMessage
                };
          }

Regards
Raghu Ram

Like

3 comments

Dear Raghu,

In order for the functionality to start working, you need to either place an event listener, which will be listening for the column value change, or call the parent implementation of basic validator method. You can do that in two ways.

First approach is based on our basic functionality: addColumnValidator() method. I can see, that you have chosen this way, though, you need to call addColumnValidator inside the setValidationConfig method. This is due to the fact, that the parent implementation of the setValidationConfig() method must be called before calling the addColumnValidator() method to correctly initialize validators of the base page fields.

setValidationConfig: function() {
   // Calls the initialization of validators for the parent view model.
   this.callParent(arguments);
    // The dobValidator() validate method is added to the [UsrDateofbirth] column.
    this.addColumnValidator("UsrDateofbirth", this.dobValidator);
}

Second approach does not use basic validator functionality. You can write a validation method (like dobValidator()) and add change event listener to the onEntityInitialized function. In such way you indicate, that once the column value gets changed(inserted), the system will run the custom validator method. In the method block, please add the following:

onEntityInitialized: function() {
   this.callParent(arguments);
   //which [column] should trigger a validation [method]
   this.on("change:[column]", this.[method], this);
}

Regards,

Anastasia

Anastasia Botezat,

Hi Sir/Madam,

I have called addColumnValidator inside the setValidationConfig method still I'm not getting the error message

 

  methods: { 
 
        setValidationConfig: function() {                
                this.callParent(arguments);
                this.addColumnValidator("UsrDOB", this.dobValidator);                
        },   
 
        dobValidator: function(value) {
                var invalidMessage = "";
                var isValid = true;
                var number = value || this.get("UsrDateofbirth");
 
                var nd = number.substr(0, 2);
                var nm = number.substr(3, 2);
                var ny = number.substr(6, 2);
                var cnumber = "";
                cnumber = nm + "-" + nd + "-" + ny;
 
                var today = new Date();
                var birthDate = new Date(cnumber);
 
                //day
                var tdate = today.toString();
                var td = tdate.substr(8, 2);
                td = parseInt(td, td);
                var bd = number.substr(0, 2);
                bd = parseInt(bd, bd);
                var d = bd - td;
 
                //year
                var age = today.getFullYear() - birthDate.getFullYear();
 
                //month
                var todayMonth = today.getMonth() + 1;
                var getMonthName =  birthDate.toString();
                var monthName = getMonthName.substr(4, 3);
                var bdmonthnumber;
 
                todayMonth = parseInt(todayMonth, todayMonth);
 
                if (monthName === "Jan") {
                    bdmonthnumber = 1;
                } else if (monthName === "Feb") {
                    bdmonthnumber = 2;
                } else if (monthName === "Mar") {
                    bdmonthnumber = 3;
                } else if (monthName === "Apr") {
                    bdmonthnumber = 4;
                } else if (monthName === "May") {
                    bdmonthnumber = 5;
                } else if (monthName === "Jun") {
                    bdmonthnumber = 6;
                } else if (monthName === "Jul") {
                    bdmonthnumber = 7;
                } else if (monthName === "Aug") {
                    bdmonthnumber = 8;
                } else if (monthName === "Sep") {
                    bdmonthnumber = 9;
                } else if (monthName === "Oct") {
                    bdmonthnumber = 10;
                } else if (monthName === "Nov") {
                    bdmonthnumber = 11;
                } else if (monthName === "Dec") {
                    bdmonthnumber = 12;
                }
 
                var m = todayMonth - bdmonthnumber;
                //console.log("m"+m);
                if ((age === 18) &amp;&amp; (m === 0) &amp;&amp; (d === 0)) {
                    isValid = true;
                } else if ((age === 18) &amp;&amp; (m === 0) &amp;&amp; (d &lt; 0)) {
                    isValid = false;
                } else if ((age === 18) &amp;&amp; (m === 0) &amp;&amp; (d &gt; 0)) {
                    isValid = true;
                } else if (age &lt; 18) {
                    isValid = false;
                } else if (age &gt; 18) {
                    isValid = true;
                }
 
                if (!isValid) {
                    invalidMessage = this.get("Resources.Strings.InvalidDOBFormat");
                }
                // Object which properties contain validation error messages.
                // If the validation is successful, empty strings are returned to the object.
                return {
                    invalidMessage: invalidMessage
                };
        }
}

Regards

Raghu Ram

Dear Raghu,

The error message does not appear, because of the errors in the validation function. Please consider rewriting the birthDay variable, since it passes date to new Date() method in a wrong format.

Sincerely,

Anastasia Botezat

Log in or register to comment