Detecting if an input has been modified

Hi! I am wondering if there is an easy way to detect whether the user has modified the value a NumberField. Thanks!


Welkom back to the community forum.

In the top right corner, you can see a full history of all saved changes made to an entity:

You can check you unsaved changes using when you press ‘Opslaan/save button’:

and check the changes:

This shows all changes you have currently made to the entity (before saving them).

Hope this answers your question.

Hi! thanks for the reply!

Sorry my question wasn’t very clear. What I meant was if it is possible to detect it using code, something like:

If NumberField_value_changed = True:
do something

Hi Yang,

You could make use of an api call to the current entity to get the last saved params and compare them with the current incoming ones:

from viktor.api_v1 import API

class Controller(ViktorController):

    def controller_method(self, params, entity_id, **kwargs):
        current_value = params.number
        saved_value = API().get_entity(entity_id).last_saved_params.number
        if current_value != saved_value:
            # do something

hi @Yang , could you explain what you would like to achieve with this?

Thank you! I’ll try this out!

Hmm it is a bit hard to explain… my app allows the user to go back and forth between different steps, and change their inputs to iteratively find the best result.

So this a function that I wrote:

def alignment(self) -> GeoPolyline:
    if self.params.elevation.geoline is not None:
        return self.params.elevation.geoline
        return convert_linestring_to_geo_polyline(self.alignment_line)

It is possible that the user created a geoline, but later changes their mind and want to use the alignment_line instead. Now the geoline is always not None. So I want to find a way to reset it back to None.

Maybe you can use a SetParamsButton to clear the field which is conditionally visible if the other fields is set. In pseudo code (no guarantees this works :stuck_out_tongue:).

Would this suit your usecase?

# in parametrization
step1.geoline = GeoLine('My geoline')
step2.reset_geoline = SetParamsButton('Reset geoline', 'clear_geoline', visible=IsNotNone(Lookup('step1.geoline')))

# in controller:
def clear_geoline(self, params, **kwargs):
    return SetParamsResult({'step1': {'geoline': None}} )

thanks! this might work, I’ll try it out :slight_smile: