Skip to content

Widget input validation #3717

Open
Open
@jimenezj8

Description

@jimenezj8

Problem

I found the response in this ticket to be inaccurate when implemented myself. When I use observe to register a validation handler, changes are made to the widget value before the change dict is passed for validation. This wouldn't be a problem except that returning change['old'] still triggers the handler again.

If I want to display a message to the user, which will clear after any value change occurs, this is not possible under the current pattern. Using the example from the ticket linked above:

from ipywidgets import FloatText

text = FloatText(value=50, continuous_update=False)

def validate(change):
    # Put your validation condition here
    print(f'validating {change["new"]}')
    if change['new'] > 50:
        change['owner'].value = change['old']

text.observe(validate, 'value')

text

Changing the value to 51 (by clicking into the text box with my mouse and hitting the up arrow key once) produces the following output:

validating 51.0
validating 50.0

Assuming that the validating is not this simple (e.g. requires a call to a method, as in my case), it's not suitable to require that the previous input, which has already presumably been validated, undergo the validation again.

Suggested Improvement

I think a full working example of validation outlining how we might extend a Widget using a custom handler would be extremely helpful. If the behavior demonstrated above is a bug, I'd be happy to file a bug report instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions