Two validations on one form

I'm trying to validate both the attachments in the first part (which works) and validate that 2 fields aren't the same.

// Validation
fd.field('Attachments').validators.push({
name: 'AttachmentsValidation',
error: "Please attach PDF, JPG or PNG documents only",
validate: function() {
for(i = 0; i < fd.field('Attachments').value.length; i++) {
if (fd.field('Attachments').value[i].extension === '.png' || fd.field('Attachments').value[i].extension === '.pdf' || fd.field('Attachments').value[i].extension === '.jpg' || fd.field('Attachments').value[i].extension === '.jpeg' || fd.field('Attachments').value[i].extension === '.PDF' || fd.field('Attachments').value[i].extension === '.JPEG' || fd.field('Attachments').value[i].extension === '.JPG' || fd.field('Attachments').value[i].extension === '.PNG') {
return true;
}
return false;
}
},
{
name: 'contactDoesNotMatchApprover',
error: "Contact and Approver cannot be the same person. Please select another approver.",
validate: function() {
if (fd.field('Contact').value === fd.field('Approver').value) {
return false;
}
return true;
}
}
});

In the second part I want the validator to check if 'Contact' and 'Approver' values are the same. If they are the same, I need to return an error message.

The Contact is a person field and the Approver is a text field.

Hello @SClacherty1,
I think there is error in the above code,

if you want to achieve two validation, try below code,

fd.validators;

fd.validators.push({

    name: 'Attachments Validator',

    error: 'Error message',

    validate: function () {

        if (fd.field('Attachments').value[i].extension != '.png' || fd.field('Attachments').value[i].extension != '.pdf' || fd.field('Attachments').value[i].extension != '.jpg' || fd.field('Attachments').value[i].extension != '.jpeg' || fd.field('Attachments').value[i].extension != '.PDF' || fd.field('Attachments').value[i].extension != '.JPEG' || fd.field('Attachments').value[i].extension != '.JPG' || fd.field('Attachments').value[i].extension != '.PNG') {

            this.error = "Please attach PDF, JPG or PNG documents only";

            return false;

        }

        if(fd.field('Contact').value === fd.field('Approver').value)

        {

            this.error="Contact and Approver cannot be the same person. Please select another approver.";

            return false;

        }

    }

}); 

Same way you can use more than one valiation with below code,

fd.spRendered(function(){
    fd.validators.push({
        name: 'MyCustomValidator_01',
        error: "Title field is required",
        validate: function(value) {
            if (!fd.field('Title').value)
                return false;

            return true;
        }
    },
    {    
        name: 'MyCustomValidator_01',
        error: "Description field is required",
        validate: function(value) {
            if (!fd.field('Description').value)
                return false;

            return true;
        }
    });
});

Hope you found this useful....!
Have a great day...!

Thanks @harshp924

This has broken all the code on my form :neutral_face:

As I mentioned, the first part works.

// Validation of Attachment
fd.field('Attachments').validators.push({
name: 'AttachmentsValidation',
error: "Please attach PDF, JPG or PNG documents only",
validate: function() {
for(i = 0; i < fd.field('Attachments').value.length; i++) {
if (fd.field('Attachments').value[i].extension === '.png' || fd.field('Attachments').value[i].extension === '.pdf' || fd.field('Attachments').value[i].extension === '.jpg' || fd.field('Attachments').value[i].extension === '.jpeg' || fd.field('Attachments').value[i].extension === '.PDF' || fd.field('Attachments').value[i].extension === '.JPEG' || fd.field('Attachments').value[i].extension === '.JPG' || fd.field('Attachments').value[i].extension === '.PNG') {
return true;
}
return false;
}
}
});

I think I get what you are saying; however I've seen a number of references to validation on this community that all deal with validation slightly differently. I want to know how to get my second validation, the Contact and Approver being different, to work.

Hello @SClacherty1,

You can either use field validators or form validators fd.validators, as suggested by @harshp924. This is up to you.

If you want to use field validators, you need to specify the validation conditions for each field separately, like this:

//validator for Attachments field
fd.field('Attachments').validators.push({
name: 'AttachmentsValidation',
error: "Please attach PDF, JPG or PNG documents only",
validate: function() {
for(i = 0; i < fd.field('Attachments').value.length; i++) {
if (fd.field('Attachments').value[i].extension === '.png' || fd.field('Attachments').value[i].extension === '.pdf' || fd.field('Attachments').value[i].extension === '.jpg' || fd.field('Attachments').value[i].extension === '.jpeg' || fd.field('Attachments').value[i].extension === '.PDF' || fd.field('Attachments').value[i].extension === '.JPEG' || fd.field('Attachments').value[i].extension === '.JPG' || fd.field('Attachments').value[i].extension === '.PNG') {
return true;
}
return false;
}
}
});

//validator for Person or Group field
fd.field('Contact').validators.push({
name: 'contactDoesNotMatchApprover',
error: "Contact and Approver cannot be the same person. Please select another approver.",
validate: function() {
if (fd.field('Contact').value && fd.field('Approver').value && fd.field('Approver').value.Key == fd.field('Contact').value.Key) {
return false;
}
return true;
}
});

Please see the example of the form validator in Manipulate field properties and appearance article