Variable and Number Field Computation

Hi Guys,

Just want to ask, is the below a correct syntax for variable and number field computation:

var VoteTotal = fd.field('numberfield1').value + fd.field('numberfield2').value + fd.field('numberfield3').value;

Apologies as I'm a newbie in JavaScript. I'm trying to accomplish a validation wherein if an inputted VotersShares (number field) is less than VoteTotal (var above), when the user clicks Submit button, it will give an error message.

Thank you in advance.

Hello @Ryan,

Welcome to Plumsail Community!

Yes, this is the correct way to sum values.

And the code to validate the form in your case would look like this:

fd.rendered(function() {

fd.validators.push({
        name: 'Error Name',
        error: "Error text",
        validate: function(value) {
            var voteTotal = fd.field('numberfield1').value + fd.field('numberfield2').value + fd.field('numberfield3').value;
            
            if (voteTotal > fd.field('VotersShares').value) {
                return false;
            } 
            return true;
        }
    });
}); 

You can find more information on how to manipulate field properties and appearance in oud documentation here.

1 Like

Thanks Nikita for the usual support and the code.

I also did a beforeSave and added the condition and throws an error when the condition is not met.

Works also. Error displays after clicking Submit button.

Thanks again.

Hi again @mnikitina, is there a way I can show the value of the below expression on a number or plain text field?

var VotingPower2 = fd.field('VotersShares').value * 7;

I'm trying the below but it does not show the value in the number field when called using onChange...Because it does not show.

fd.field('VotingNumber').value = VotingPower2;

Thank you

Hi @mnikitina... I sorted it out. It seems that this cannot be done when calling an OnChange. I tried to insert the code in beforeSave and it worked. I think a button is needed for this to work. Is there a way you can call this without a button? Thanks again...

Hello @Ryan,

Do you need to update the value when VotersShares field is changed?

For this, you can use this code:

fd.rendered(function() {
   
  fd.field('VotersShares').$on('change', function(value) {
      var VotingPower2 = fd.field('VotersShares').value * 7;
      fd.field('VotingNumber').value = VotingPower2;
  });

}); 

Please make sure you use the internal names of the fields in the code.

1 Like

Thanks much @mnikitina. Appreciate it. :blush:

1 Like