Make Field Required based on another field

I know how to do this with dates but looking for some help with just using fields.

I want to make field A required when field B is equal to a specific value.

1 Like

Dear Stefanie,
You can try custom validators, something like this:

fd.field('Field2').validators.push({
    name: 'MyCustomValidator',
    error: '',
    validate: function(value) {
        if (fd.field('Field1').value == "Value" && !fd.field('Field2').value) {
            this.error = 'If Field1 equals "Value", fill Field2 as well!';
            return false;
        }

        return true;
    }
});

I am using this and it is not working

fd.spRendered(function() {
  fd.field('Quote_x0020_MRR').validators.push({
    name: 'MyCustomValidator',
    error: '',
    validate: function(value) {
        if (fd.field('Status').value == 'Contract Sent' && !fd.field('Quote_x0020_MRR').value) {
            this.error = 'Quote MRR Required at this Status';
            return false;
        }

        return true;
    }
});

Dear Stefanie,
Normally, this should work, but it depends on what fields you are using.

For example, what’s the type of Status field? If it’s Choice - then it should work, if it’s Lookup - then it wouldn’t work.

Status is a choice column. So if status = Contract Sent I want Quote MRR to be required.
Quote MRR is a number column.
Do I have an error in the code?

Dear Stefanie,
The code does work for me with these types of fields, though you might be missing the closing brackets (you need two):

fd.spRendered(function() {
  fd.field('Quote_x0020_MRR').validators.push({
    ...
    }
  });
});

Thank you! That was it, I appreciate your help

Hi, I am trying to do something quite similar. I want to require a reject reason (multi select check box field) when the status (a drop down) is Rejected. This is the code I have and it is not working. Could you help me out? Thank you!!

fd.spRendered(function() {
fd.field('MgrRejectReason').validators.push({
    name: 'MyCustomValidator',
    error: '',
    validate: function(value) {
        if (fd.field('MgrReimbursementStatus').value == "Rejected" && !fd.field('MgrRejectReason').value) {
            this.error = 'Missing Reject Reason';
            return false;
        }
        return true;
    }
});
});

Dear @caria,
For multi select checkbox, try to check the value's length, like this:

fd.spRendered(function() {
fd.field('MgrRejectReason').validators.push({
    name: 'MyCustomValidator',
    error: '',
    validate: function(value) {
        if (fd.field('MgrReimbursementStatus').value == "Rejected" && fd.field('MgrRejectReason').value.length == 0) {
            this.error = 'Missing Reject Reason';
            return false;
        }
        return true;
    }
});
});
1 Like

Thank you that worked!!

1 Like

Hi, can you help me with another similar situation? I have a choice field (EscortRequired) and a person picker field (EscortName), when they select yes for escort required, I want to require the person picker field.

I am using this code but it is not working:

fd.spRendered(function() {
fd.field('EscortName').validators.push({
    name: 'MyCustomValidator',
    error: '',
    validate: function(value) {
        if (fd.field('EscortRequired').value == "Yes" && !fd.field('EscortName').value) {
            this.error = 'Missing Escort Name';
            return false;
        }

        return true;
    }
});
});

Thank you!

Dear @caria,
What's the type of field for EscortRequired? If it's Yes/No field then it has no text value, only true/false, and also field can be made required easier, like this:

fd.spRendered(function() {

    function setEscortNameRequired() {
        if (fd.field('EscortRequired').value) {
            // Set EscortName required
            fd.field('EscortName').required = true;
        } else {
            // Set EscortName as not required
            fd.field('EscortName').required = false;
        }
    }

    // Calling setEscortNameRequired when the value changes
    fd.field('EscortRequired').$on('change',setEscortNameRequired);

    // Calling setEscortNameRequired on form loading
    setEscortNameRequired();

});

It is a choice field, with Yes and No as radio buttons.

Thank you!! I will try that. If in the form they change escort required to no, will it then let them save the form without the escort name?

Essentially if the escort required field equals yes, then I want to require the escort name.

Dear @caria,
In case it's a Choice field, you do need to check for its value as text:

fd.spRendered(function() {

    function setEscortNameRequired() {
        if (fd.field('EscortRequired').value == 'Yes') {
            // Set EscortName required
            fd.field('EscortName').required = true;
        } else {
            // Set EscortName as not required
            fd.field('EscortName').required = false;
        }
    }

    // Calling setEscortNameRequired when the value changes
    fd.field('EscortRequired').$on('change',setEscortNameRequired);

    // Calling setEscortNameRequired on form loading
    setEscortNameRequired();

});
1 Like

Dear @Nikita_Kurguzov

THANK YOU SO MUCH! That worked perfectly, even better than what I was trying to do!!

1 Like