Validation Efforts on Datatable

I am currently working on a annual violation validation form that has a data table that the driver will input their driving violations for the year. The functionality will go as below:

  1. Column 1 Date of violation *Required
  2. Column 2 Violation type *Required
  3. Column 3 If "Other" is selected on Column 2 then this field needs a value otherwise not. I'd like it read only and go to required if Other is selected but I can't get that to work for the life of me so at minimum I am checking for a value.
  4. Column 4 Location *Required.

I have the start of the JavaScript that is only partially working. (Pasted below) Any help is appreciated.

fd.control('ViolationTable').addValidator({
name: 'DataTable validator',
error: "You selected OTHER on a violation and didn't put in a value.",
validate: function(value){
for(var i = 0; i < value.length; i++){
if(value[i].Violation == 'Other (Type in next field)' && !value[i].Other){
return false;
}
}
fd.control('ViolationTable').addValidator({
name: 'DataTable validator',
error: "Missing a location on a record.",
validate: function(value){
for(var i = 0; i < value.length; i++){
if(!value[i].Location){
return false;
}
}
return true;
}});
}});

Hello @IT.Joe,

You are on the right track. You have few problems:

  • missing return true in the first function;
  • closing brackets are missing for the first function.

I've updated the code, please have a look:

fd.control('ViolationTable').addValidator({
    name: 'DataTable validator',
    error: "You selected OTHER on a violation and didn't put in a value.",
    validate: function(value){
        for(var i = 0; i < value.length; i++){
            if(value[i].Violation == 'Other (Type in next field)' && !value[i].Other){
                return false;
            }
    }
    return true;
    }
});

fd.control('ViolationTable').addValidator({
    name: 'DataTable validator',
    error: "Missing a location on a record.",
    validate: function(value){
        for(var i = 0; i < value.length; i++){
            if(!value[i].Location){
                return false;
            }
        }
    return true;
    }
});

Thank you that worked great

1 Like