Require Fields based on field value

I am trying to make certain fields required only when a field matches a certain value. This is a form that is on a SharePoint site.

I have read various articles on how to do this but for some reason nothing is happening on my form.

Below is my code. Could someone point out what I am doing wrong here? Based on what I have read everything looks correct to me.

fd.spRendered(function() {
    fd.field('Type_x0020_of_x0020_Request').validators.push({
        name: 'InitialUser',
        error: "This is a required field",
        validate: function(value) {
                //If type of request is equal to Initial then set the below fields to required
            if (fd.field('Type_x0020_of_x0020_Request').value = 'Initial' {
        
                fd.field('EmployeeStartDate').required = true;
                fd.field('EmployeePosition').required = true;
                fd.field('Employee_x0020_Department').required = true;
                fd.field('Phone').required = true;
                fd.field('DID').required = true;
                fd.field('Should_x0020_Employee_x0020_be_x').required = true;
                fd.field('ID_x0020_Badge').required = true;
            
                this.error = "This is a required field";
                }
            
        return true;
    }           
        
});

});

Dear @Nazman1126 ,
Hello and welcome, thank you for posting! There are a couple ways you can approach this - one method is to make a validator, like you do here. The validators are working better when multiple conditions have to be met, for example, the dates have to be within certain range of each other, or in certain conditions, something has to be different, etc.

If you simply want to set fields to required state, something like this would be simpler and more efficient:

fd.spRendered(function() {

    function checkTypeOfRequest() {
        if (fd.field('Type_x0020_of_x0020_Request').value == 'Initial') {
            fd.field('EmployeeStartDate').required = true;
            fd.field('EmployeePosition').required = true;
            fd.field('Employee_x0020_Department').required = true;
            fd.field('Phone').required = true;
            fd.field('DID').required = true;
            fd.field('Should_x0020_Employee_x0020_be_x').required = true;
            fd.field('ID_x0020_Badge').required = true;
        } else {
            fd.field('EmployeeStartDate').required = false;
            fd.field('EmployeePosition').required = false;
            fd.field('Employee_x0020_Department').required = false;
            fd.field('Phone').required = false;
            fd.field('DID').required = false;
            fd.field('Should_x0020_Employee_x0020_be_x').required = false;
            fd.field('ID_x0020_Badge').required = false;
        }
    }

    // Calling checkTypeOfRequest when the user changes the Type of Request
    fd.field('Type_x0020_of_x0020_Request').$on('change',checkTypeOfRequest);

    // Calling checkTypeOfRequest on form loading
    checkTypeOfRequest();

});

@Nikita_Kurguzov,

I didn't see any code like that when I was doing research. Thank you very much for this.
That is much more simpler than what I had and I tested it and it works great. This is the first time I have used Plumsail and this helps a lot.

Thank you very much.

Dear @Nazman1126,
You're very welcome! There is no shame in asking, so, please, post any questions you might have - we'll be happy to assist!

Maybe one day it will be you, helping others in the community to learn the ropes :wink:

1 Like