AnsweredAssumed Answered

How to dynamically require a field based on another field.

Question asked by Paul Carlton on Mar 15, 2016
Latest reply on Mar 17, 2016 by Mark Viglianco

Can't seem to find anything on this.

 

I want to dynamically require another field based on a value set in a different field BEFORE saving the record.

 

Lets say we're in Accounts and there's a custom field say "how_far_c"  and another custom field "too_far_c".  If how_far_c is set to > 100, than "too_far_c" is required.  It shouldn't matter what kind of field either of them are but lets say too_far_c is a dropdown for "Yes" and "No".  If how_far_c is < 100, then too_far_c is not required to answer.  How do I do that?

 

Right now I'm using something like:

 

initialize: function(options) {

....

this.model.on('change:how_far_c', this.howFar, this);

}

 

howFar: function() {

  if (this.model.get('how_far_c') > 100) {

    this.model.fields['too_far_c']['required'] = true;

  }

}

 

But this doesn't work outside the initialize function.  If I call required = true in initialize, it works from the get go.  The way I'm doing it above is instead shaky, it seems to take hold AFTER saving.  Meaning, it only requires it if I save it and then try and modify how_far_c again.  Is there a better way of doing this??

Outcomes