Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Input.error property #894

Merged
merged 10 commits into from
Jun 19, 2023
Merged

Conversation

itay-raveh
Copy link
Contributor

@itay-raveh itay-raveh commented May 4, 2023

Added an error property to the Input element which always contains the latest error message from the given validators.

Motivation

Say we have the following ui.input setup:

inp = ui.input(label=..., validation={'Input too long': lambda value: len(value) < 20})
def on_submit():
    ... # content of following code blocks here
inp.on("keydown.enter", on_submit)

Say in the on_submit callback we want to base behavior on the error state.

How would that look before and after this PR?

Checking if an error is active

Before

# access to "private" member :(
if "error-message" in inp._props:
    ... # do something with `inp.value`

After

if inp.error:
    ... # do something with `inp.value`

Checking if a specific error is active

Before

# access to "private" member :(
if inp._props.get("error-message") == "Input too long":
    ... # do something with `inp.value`

After

if inp.error == "Input too long":
    ... # do something with `inp.value`

@rodja rodja added the enhancement New feature or request label May 5, 2023
@rodja rodja added this to the 1.2.12 milestone May 5, 2023
@falkoschindler falkoschindler modified the milestones: 1.2.12, 1.2.13 May 5, 2023
@rodja rodja modified the milestones: 1.2.13, v1.2.14 May 5, 2023
@rodja rodja modified the milestones: 1.2.14, 1.2.15 May 14, 2023
@falkoschindler falkoschindler modified the milestones: 1.2.15, 1.2.16 May 26, 2023
@rodja rodja modified the milestones: 1.2.16, 1.2.17 May 31, 2023
@rodja rodja modified the milestones: 1.2.17, 1.2.18 Jun 5, 2023
@rodja rodja modified the milestones: 1.2.18, 1.2.19, 1.9.20 Jun 12, 2023
Copy link
Contributor

@falkoschindler falkoschindler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I finally looked into your PR, @itay-raveh!

In order to use this feature across all input elements with validation (input, number, textarea), I refactored it into a mixin. This nicely separates the validation code from other concerns.

Regarding the change to validation being an Optional[Dict] rather than a possibly empty Dict: I'm not quite convinced that we need this change. Therefore I switched back to a Dict. But maybe there is an argument I missed.

And I'm thinking about whether the property name error is to generic and should be something like validation_error. Once we release a new feature with a certain name, it's always tricky to change it later.

@falkoschindler
Copy link
Contributor

Oh, there's another point we need to think about: How to integrate error with ui.number's min/max limits? There has been a feature request #924 for an is_valid property, which might combine validation, min and max. But how does it integrate with an error of type Optional[str]?

Maybe we simply add out_of_limits to ui.number and the user can decide which one to use, e.g.

i = ui.number(min=0, max=100, value=50, validation={'Too small!': lambda value: value > 80})
...
if i.error or i.out_of_limits:
    ui.notify('Something is wrong.')

@falkoschindler falkoschindler modified the milestones: 1.2.21, 1.2.22 Jun 16, 2023
@falkoschindler falkoschindler merged commit 149662c into zauberzeug:main Jun 19, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants