Validate fields based on Multiselect drop-down selection

Hi Plumsail!

I have a drop-down with a multi-select option enabled.

IF users select more than one option from the drop-down, it will show tabs in Wizard 3 which encompass all the fields.

image

I have set-up some simple logic where if a user select one of the options in the image above, it will validate all the fields under that tab.

My problem is, if user's multiselect the options, it won't validate anything. By extension, IF users select Personal Details && Home Address, the validation won't kick in if I hit "Next" on Wizard Step change.

Here is the code that I thought would work but isn't doing anything:

//This code will validate the New Name field - [Personal Details]
fd.field('New_Name').validators.push({
    name: '',
    error: "Please enter the New Name",
    validate: function(value) {
           //User selection
        if (fd.field('Question_2').value == 'Personal Details' && fd.field('Question_2').value == 'Home Address'
        //field under tab 1
        && fd.field('New_Name').value.length == 0) {
        return false;
        }
        return true;
        }
});

Dear @DryChips,
That's because the value is an array of all selected options, not a string. You can check it like this:

if (fd.field('Question_2').value.indexOf('Personal Details') >= 0 && fd.field('Question_2').indexOf('Home Address') >= 0){
  //do something
}
1 Like

Awesome, thanks as always Nikita! :smiley:

большо́е спаси́бо :sunglasses:

1 Like

Sorry Nikita,

I have come across a strange issue when running my code.

In this snippet, the function doesn't run, it returns an error in console:

//This code will validate the New Name field - [Personal Details & Home Address]
fd.field('New_Name').validators.push({
    name: '',
    error: "Please enter the New Name",
    validate: function(value) {
        if (fd.field('Question_2').value.indexOf('Personal Details')>=0 && fd.field('Question_2').value.indexOf('Home Address')>=0){
        fd.field('New_Name').value.length == 0;

        return false;
        }
        return true;
        }
});

If I run this code snippet:

//This code will validate the New Name field - [Personal Details & Home Address]
fd.field('New_Name').validators.push({
    name: '',
    error: "Please enter the New Name",
    validate: function(value) {
        if (fd.field('Question_2').value.indexOf('Personal Details')>=0 && fd.field('Question_2').value.indexOf('Home Address')>=0){
        fd.field('New_Name').value == null;

        return false;
        }
        return true;
        }
});

When I enter a value in the field, the error event stays with no errors in console:

image

Dear @DryChips,
Not sure what you're trying to do here, as you're not checking the last condition, you can try this:

//This code will validate the New Name field - [Personal Details & Home Address]
fd.field('New_Name').validators.push({
    name: '',
    error: "Please enter the New Name",
    validate: function(value) {
        if (fd.field('Question_2').value.indexOf('Personal Details')>=0 && fd.field('Question_2').value.indexOf('Home Address')>=0 && !fd.field('New_Name').value){
        return false;
        }
        return true;
        }
});
1 Like

Hi Nikita,

I am just checking whether the field is empty or not. If it's empty, I want it to return an error, so users can be alerted that the field needs to filled.