Validation for datatable not working properly

Hi Plumsail!

I have a datatable (dtB) that needs to be validated whether the data in specific column ('MeterNo') is present in another datatable (dtA), otherwise it should return error.

I have to put 'value.forEach' inside 'fd.control('dtA')' function because if not, it executes even when it's not done getting all the data in bList yet.

fd.control('dtB').addValidator({
            name: 'DBCustomValidator',
            error: 'Meter no is invalid',
            validate: function(value){
                        fd.control('dtA').ready().then(function(curVal) { 
                                    const bList= [];
                                    var bListValue= curVal.value;
                                    
                                    //gets all the data in bList
                                    bListValue.forEach(function (itemB) {
                                            bList.push(item.MeterNo);
                                    });
                                    
                                  //validate if itemA.MeterNo in bList
                                 value.forEach(function(itemA) {
                                        if (bList.indexOf(itemA.MeterNo) == -1) {
                                             //alert('false');
                                             return false;
                                        }
                                       else{
                                              //alert('true');
                                       }
                                 });

                                 return true;
                             });
               }
    });

It always return false, even when it didn't went into 'return false' statement, I tried to put the validation inside fd.validators too but same action happens.

Also, when I used debugger, the error already shows before the line with 'return'

Dear @Jes_Santimosa,
You probably don't need ready() there... As for the issue, try to debug with console.log:

fd.control('dtB').addValidator({
    name: 'DBCustomValidator',
    error: 'Meter no is invalid',
    validate: function(value){
            var curVal = fd.control('dtA').value;
            var bList= [];
            var bListValue= curVal.value;
            //gets all the data in bList
            bListValue.forEach(function (itemB) {
                    bList.push(item.MeterNo);
            });
            console.log(bList);
            //validate if itemA.MeterNo in bList
            value.forEach(function(itemA) {
                console.log(itemA.MeterN);
                if (bList.indexOf(itemA.MeterNo) == -1) {
                    console.log('false');
                }
                else{
                    console.log('true');
                }
            });
            return true;
        }
});

Dear @Nikita_Kurguzov,
Thanks for the reply. I still experience the issue, return false now doesn't work even it go through console console.log('false'), it successfully saves. Please see attached log ss

	fd.control('dtB').addValidator({
			name: 'DBCustomValidator',
			error: 'Meter no is invalid',
			validate: function(value){
						var curVal = fd.control('dtA').value;
						var bList= [];
						var bListValue= curVal.value;
						//gets all the data in bList
						bListValue.forEach(function (itemB) {
								bList.push(itemB.MeterNo);
						});
						//console.log(bList);
						//validate if itemA.MeterNo in bList
						
						value.forEach(function(itemA) {
							//console.log(itemA.MeterNo);
							if (bList.indexOf(itemA.MeterNo) == -1) {
								console.log('false');
								return false; //not returning
							}
							else{
								console.log('true');
							}
						});
						return true;
				}
	});

Log screenshot:
image

Dear @Jes_Santimosa,
Hmm, I think that's because you run a function within, try a regular for loop instead:

for(var i = 0; i < value.length; i++){
  //console.log(value[i].MeterNo);
  if (bList.indexOf(value[i].MeterNo) == -1) {
    console.log('false');
    return false; //not returning
  }
}
console.log('true');
return true;

Dear @Nikita_Kurguzov,

It now worked, thanks for the help!

1 Like