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

feat: added Validate function for textinput #114

Closed

Conversation

IllusionMan1212
Copy link
Contributor

@IllusionMan1212 IllusionMan1212 commented Feb 2, 2022

This PR adds a Validate function to the textinput bubble to allow for input validation before it's appended to the textinput value.

3 built-in validators are provided:

  • ValidateInt: validates integer input only.
  • ValidateFloat: validates float input.
  • ValidateBool: validates boolean-like inputs such as y, n, t, f, 1, 0 (case insensitive)

A usage example is provided here: https://gist.github.com/IllusionMan1212/acddb1533790ad6abc6ee23fac6d9709

This does not validate pasted input. But should it?

@IllusionMan1212 IllusionMan1212 force-pushed the input-validation branch 2 times, most recently from 6663f8b to 58ad2b5 Compare February 2, 2022 12:13
textinput/textinput.go Outdated Show resolved Hide resolved
textinput/textinput.go Outdated Show resolved Hide resolved
textinput/textinput.go Outdated Show resolved Hide resolved
@muesli
Copy link
Member

muesli commented Feb 5, 2022

We should also think of a way to notify API-users of validation issues, so they can inform the user of an error in their program.

@meowgorithm
Copy link
Member

meowgorithm commented Feb 6, 2022

Just chiming in that we should definitely validate pasted input too.

@IllusionMan1212
Copy link
Contributor Author

Updated to accommodate the requested changes

@maaslalani
Copy link
Member

Hi @IllusionMan1212, great idea and great work on this PR.
We're likely going to merge these changes in #167.
Thank you so much!

@maaslalani maaslalani closed this Jun 10, 2022
GabrielNagy added a commit to GabrielNagy/bubbles that referenced this pull request Jul 5, 2022
This PR builds upon the excellent work in charmbracelet#167 and charmbracelet#114 and adds a bit
more customizability to the feature.

Currently, the validation API will completely block text input if the
Validate function returns an error. This commit makes this behavior
configurable by introducing a ValidateAction which decides how to
proceed in case a validation error happens. To preserve backwards
compatibility, the default behavior remains unchanged.

If ValidateAction is set to AllowInput, the text input error will still
be set if needed, but actual input will not be blocked.

This is helpful for cases where the user is requested to type an
existing system path, and the Validate function keeps asserting this.
With the current implementation such a validation is not possible.

For example:

    > /
    Err: nil

    > /t
    Err: /t: No such file or directory

    > /tm
    Err: /tm: No such file or directory

    > /tmp
    Err: nil
GabrielNagy added a commit to GabrielNagy/bubbles that referenced this pull request Jul 5, 2022
This PR builds upon the excellent work in charmbracelet#167 and charmbracelet#114 and adds a bit
more customizability to the feature.

Currently, the validation API will completely block text input if the
Validate function returns an error. This commit makes this behavior
configurable by introducing a ValidateAction which decides how to
proceed in case a validation error happens. To preserve backwards
compatibility, the default behavior remains unchanged.

If ValidateAction is set to AllowInput, the text input error will still
be set if needed, but actual input will not be blocked.

This is helpful for cases where the user is requested to type an
existing system path, and the Validate function keeps asserting this.
With the current implementation such a validation is not possible.

For example:

    > /
    Err: nil

    > /t
    Err: /t: No such file or directory

    > /tm
    Err: /tm: No such file or directory

    > /tmp
    Err: nil
GabrielNagy added a commit to GabrielNagy/bubbles that referenced this pull request Jul 5, 2022
This PR builds upon the excellent work in charmbracelet#167 and charmbracelet#114 and adds a bit
more customizability to the feature.

Currently, the validation API will completely block text input if the
Validate function returns an error. This commit makes this behavior
configurable by introducing a ValidateAction which decides how to
proceed in case a validation error happens. To preserve backwards
compatibility, the default behavior remains unchanged.

If ValidateAction is set to AllowInput, the text input error will still
be set if needed, but actual input will not be blocked.

This is helpful for cases where the user is requested to type an
existing system path, and the Validate function keeps asserting the
existence of the path. With the current implementation such a validation
is not possible.

For example:

    > /
    Err: nil

    > /t
    Err: /t: No such file or directory

    > /tm
    Err: /tm: No such file or directory

    > /tmp
    Err: nil
GabrielNagy added a commit to GabrielNagy/bubbles that referenced this pull request Aug 18, 2022
This PR builds upon the excellent work in charmbracelet#167 and charmbracelet#114 and adds a bit
more customizability to the feature.

Currently, the validation API will completely block text input if the
Validate function returns an error. This commit makes this behavior
configurable by introducing a ValidateAction which decides how to
proceed in case a validation error happens. To preserve backwards
compatibility, the default behavior remains unchanged.

If ValidateAction is set to AllowInput, the text input error will still
be set if needed, but actual input will not be blocked.

This is helpful for cases where the user is requested to type an
existing system path, and the Validate function keeps asserting the
existence of the path. With the current implementation such a validation
is not possible.

For example:

    > /
    Err: nil

    > /t
    Err: /t: No such file or directory

    > /tm
    Err: /tm: No such file or directory

    > /tmp
    Err: nil
GabrielNagy added a commit to GabrielNagy/bubbles that referenced this pull request Sep 4, 2023
This PR builds upon the excellent work in charmbracelet#167 and charmbracelet#114 and adds a bit
more customizability to the feature.

Currently, the validation API will completely block text input if the
Validate function returns an error. This commit makes a breaking change
to the ValidateFunc by returning an additonal bool that indicates
whether or not input should be blocked.

This is helpful for cases where the user is requested to type an
existing system path, and the Validate function keeps asserting the
existence of the path. With the current implementation such a validation
is not possible.

For example:

    > /
    Err: nil

    > /t
    Err: /t: No such file or directory

    > /tm
    Err: /tm: No such file or directory

    > /tmp
    Err: nil
GabrielNagy added a commit to GabrielNagy/bubbles that referenced this pull request Feb 29, 2024
This PR builds upon the excellent work in charmbracelet#167 and charmbracelet#114 and adds a bit
more customizability to the feature.

Currently, the validation API will completely block text input if the
Validate function returns an error. This commit makes a breaking change
to the validate API to no longer block input if this is the case, thus
handing this responsibility to the clients.

This is helpful for cases where the user is requested to type an
existing system path, and the Validate function keeps asserting the
existence of the path. With the current implementation such a validation
is not possible.

For example:

    > /
    Err: nil

    > /t
    Err: /t: No such file or directory

    > /tm
    Err: /tm: No such file or directory

    > /tmp
    Err: nil
GabrielNagy added a commit to GabrielNagy/bubbles that referenced this pull request Feb 29, 2024
This PR builds upon the excellent work in charmbracelet#167 and charmbracelet#114 and makes a
breaking change to the validation API.

Currently, validation will completely block text input if the Validate
function returns an error. This is now changed so the function no longer
blocks input if this is the case, thus handing this responsibility to
the clients.

This is helpful for cases where the user is requested to type an
existing system path, and the Validate function keeps asserting the
existence of the path. With the current implementation such a validation
is not possible.

For example:

    > /
    Err: nil

    > /t
    Err: /t: No such file or directory

    > /tm
    Err: /tm: No such file or directory

    > /tmp
    Err: nil
maaslalani pushed a commit to GabrielNagy/bubbles that referenced this pull request Mar 13, 2024
This PR builds upon the excellent work in charmbracelet#167 and charmbracelet#114 and makes a
breaking change to the validation API.

Currently, validation will completely block text input if the Validate
function returns an error. This is now changed so the function no longer
blocks input if this is the case, thus handing this responsibility to
the clients.

This is helpful for cases where the user is requested to type an
existing system path, and the Validate function keeps asserting the
existence of the path. With the current implementation such a validation
is not possible.

For example:

    > /
    Err: nil

    > /t
    Err: /t: No such file or directory

    > /tm
    Err: /tm: No such file or directory

    > /tmp
    Err: nil
maaslalani added a commit that referenced this pull request Mar 14, 2024
* feat(textinput): do not block input on validation

This PR builds upon the excellent work in #167 and #114 and makes a
breaking change to the validation API.

Currently, validation will completely block text input if the Validate
function returns an error. This is now changed so the function no longer
blocks input if this is the case, thus handing this responsibility to
the clients.

This is helpful for cases where the user is requested to type an
existing system path, and the Validate function keeps asserting the
existence of the path. With the current implementation such a validation
is not possible.

For example:

    > /
    Err: nil

    > /t
    Err: /t: No such file or directory

    > /tm
    Err: /tm: No such file or directory

    > /tmp
    Err: nil

* fix: change name

---------

Co-authored-by: Maas Lalani <maas@lalani.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants