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

[0.5.x] Re-work on-change validation #105

Merged
merged 4 commits into from
Nov 8, 2024
Merged

[0.5.x] Re-work on-change validation #105

merged 4 commits into from
Nov 8, 2024

Conversation

timacdonald
Copy link
Member

@timacdonald timacdonald commented Nov 7, 2024

Precognition does its best to avoid sending requests when it does not need to. This might happen when the validation function has been called but the form's has not changed since the last validation attempt.

You could imagine a text input where the user changes a value and before leaving the field restores the original value. In that scenario there is no need to send a validation request as the form has not changed and the existing validation errors etc still makes sense.

This is the expected behaviour when you are validating a single field and the user is implicitly invoking the validate function.

<input
   v-model="form.email"
   @change="form.validate('name')"
>

However, when building a wizard and calling form.validate() in a button without specifying a particular input to validate, you expect that the validation process will occur regardless of the state of the form so you are able to react to the response.

<button
    type="button"
    @click="form.touch(['name', 'email', 'phone']).validate({
        only: ['name', 'email', 'phone'],
        onSuccess: (response) => nextStep(),
        onValidationError: (response) => /* ... */,
    })"
>Next Step</button>

The changes in this PR ensure the second scenario will send a validation request even when the form has not changed since the last validation attempt.

const createValidator = () => debounce((instanceConfig: Config) => {
const createValidator = () => debounce((instanceConfig: ValidationConfig) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Small type improvement.

Comment on lines -299 to +295
const validate = (name?: string | NamedInputEvent, value?: unknown, config?: Config): void => {
const validate = (name?: string | NamedInputEvent, value?: unknown, config?: ValidationConfig): void => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Small type improvement.

@timacdonald timacdonald changed the title [0.6.x] Re-work on change validation [0.6.x] Re-work on-change validation Nov 7, 2024
@timacdonald timacdonald changed the title [0.6.x] Re-work on-change validation [0.5.x] Re-work on-change validation Nov 7, 2024
@timacdonald timacdonald marked this pull request as ready for review November 7, 2024 23:59
@taylorotwell taylorotwell merged commit c198547 into main Nov 8, 2024
4 checks passed
@taylorotwell taylorotwell deleted the validate branch November 8, 2024 03:08
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.

2 participants