Disable invalid value for a setting #1284
-
| I managed to create an autocompletable settable:         env_settable = Settable('env', str, 'Your environment', self,
            choices_provider=list_envs, onchange_cb=self._env_changed)
        self.add_settable(env_settable)
    def _env_changed(self, param_name, old, new):
        if new not in list(config.get_config()['environments'].keys()):
            self.env = oldAlmost there, but is there a way to correctly refuse to set an invalid setting? FTA> set env eee
env - was: 'dev'
now: 'eee'
FTA> set env
Name  Value                           Description                                                 
==================================================================================================
env   dev                             Your environment                                            
FTA>  | 
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
| To validate a  
 Here is  So in your case, instead of setting  def validate_env(new_val: str) -> str:
    valid_vals = list(config.get_config()['environments'].keys())
    if new_val not in valid_vals:
        raise ValueError(f"invalid choice: {new_val!r} (choose from {valid_vals})")
    return new_val | 
Beta Was this translation helpful? Give feedback.
To validate a
Settable'svalue, useval_typeand notonchange_cb.val_typeis a function which runs when setting the value. Whereasonchage_cb, as you've already noticed , runs after theSettablehas been set to the new value.Here is
val_type'sdocsctring.So in your case, instead of setting
val_typetos…