Setting a dictionary on HiddenField using SetParams updates instead of override

Which SDK and Platform version are you using?

SDK: v12.5.0
Platform: v4.2.0

Current Behavior

In a method of a SetParamsButton I set the new params of a Hiddenfield, the data to store is in dictionary form. When I perform this action the first time everything is fine. But when I perform this action a second time with different parametrization the HiddenField data is expanded rather then replaced.

Expected Behavior

I would expect the data in the HiddenField to be replaced with the value I set.

Context (optional, but preferred)

Can be reproduced using:

from viktor import ViktorController
from viktor.parametrization import Parametrization, TextField, HiddenField, SetParamsButton
from viktor.result import SetParamsResult
from viktor.views import Summary

class MyParametrization(Parametrization):
    text = TextField('Text to store as key')
    hidden = HiddenField('...')
    set_params = SetParamsButton('Set', 'set_params')


class MyController(ViktorController):
    label = '...'
    summary = Summary()
    parametrization = MyParametrization

    def set_params(self, params, **kwargs):
        return SetParamsResult({
            'hidden': {
                params.text: 1
            }
        })

Hi Wouter, we do not yet have a solution unfortunately.

I would advise to work with other data formats on a hidden field for now. You could also keep the dictionary format, but store it as a json-string on the hidden field:

    def set_params(self, params, **kwargs):
        return SetParamsResult({
            'hidden': json.dumps({
                params.text: 1
            })
        })

Keep in mind to convert the json-string back to a dictionary where needed: json.loads(params['hidden'])