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

Feature: Validate Input #167

Merged
merged 1 commit into from Jun 10, 2022
Merged

Feature: Validate Input #167

merged 1 commit into from Jun 10, 2022

Conversation

maaslalani
Copy link
Member

Based on #114

Adds Validate property to textinput to ensure input is valid.

Validate is a function that accepts a string and returns an error which may be nil if the string is valid.

// ValidateFunc is a function that returns an error if the input is invalid.
type ValidateFunc func(string) error

textinput/textinput.go Outdated Show resolved Hide resolved
textinput/textinput.go Outdated Show resolved Hide resolved
textinput/textinput.go Outdated Show resolved Hide resolved
@maaslalani maaslalani force-pushed the validate branch 2 times, most recently from ad414d7 to edb33a0 Compare June 10, 2022 14:07
@maaslalani maaslalani merged commit e57fd29 into master Jun 10, 2022
@maaslalani maaslalani deleted the validate branch June 10, 2022 16:17
runes := []rune(s)
if m.CharLimit > 0 && len(runes) > m.CharLimit {
m.value = runes[:m.CharLimit]
} else {
m.value = runes
}
if m.pos == 0 || m.pos > len(m.value) {
if (m.pos == 0 && len(m.value) == 0) || m.pos > len(m.value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey @maaslalani do you know if this was intended?

Basically now when I use SetValue to prefill a field my cursor is no longer moved to the end of the line and I have to call CursorEnd myself.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hey! That's definitely a bug, I will fix it soon! Thank you for catching it.

Copy link
Contributor

Choose a reason for hiding this comment

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

No worries, if you want I can open a PR 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

If you would like to, go for it! It would be much appreciated :)

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