Question

Programmatically change column value when its value is set to something else.

I have a freeform number field that I want to disallow certain values.

Rather than adding a validation that prevents the user from saving the page, or forces them to change the value, I would like to have the value reset automatically to something else.

I tried adding an attribute that triggers a method every time the columns is changed, but trying to change the same column inside that method causes an infinite loop.

Is there any way to get this process I want of automatically resetting a value when it is set to something "invalid"?

Like 0

Like

4 comments

Hi Jordan,

It seems like the conditions on your method are allowing that infinite loop. Make sure you are not updating the field value when a "valid" entry. This should be the end of your loop.

To contribute with more details, could you, please, provide the piece of code for the attribute and the method?

Thanks. 

Dear Jordan, 

Firstly, you need to add an if clause to you custom method, which is triggered by column change. Apply the needed condition for the column to set default value. For example,

var column = this.get("Your column");

if (column !== "some value") {

   this.set(column, "default value");

} else {

  //do other logic or return false

}

In this case you won't have a loop. If you will have any issues, you can share your code with us.

Regards,

Anastasia

Anastasia Botezat,

I'll look at my implementation again, but I tried this already and it still resulted in an infinite loop.

You can also try wiring up the blur event on the number field, then fix it to a valid value when the control is exited, rather than when changed. To wire up a blur method, just add to the diff like this: 

{
    "operation": "insert",
    ...,
    "values": {
        "blur": { "bindTo": "onMyNumberFieldExit" }
    }
}

Then, implement your logic in the onMyNumberFieldExit method, this won't have issues with an infinite loop since it won't be triggered until the user leaves the field.

Ryan

Show all comments