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] Improve async validation API #109

Merged
merged 1 commit into from
Jan 13, 2025
Merged

[0.5.x] Improve async validation API #109

merged 1 commit into from
Jan 13, 2025

Conversation

timacdonald
Copy link
Member

@timacdonald timacdonald commented Dec 8, 2024

The API when working with a form wizard is less than ideal. We have done a lot of work recently to make this experience better, but the API never felt right.

You currently need to call the touch method to ensure the current page's inputs are validated. Inputs are marked as touch when a user interacts with them, but in a wizard you need to validate chunks of the form at a time even if the user has not interacted with the inputs.

<button
    onClick={() => form.touch(['name', 'email']).validate({
        onSuccess: () => nextStep(),
    })}
>
    Next Step
</button>

It is also not possible to un-touch an input, which can cause issues with certain form wizard configurations where you hide old inputs.

This PR intends to make the wizard API better while addressing the issue with not being able to un-touch inputs.

To achieve this we are exposing an only validation key as part of the documented public API. This configuration value already exists, but it is currently called validate and is not intended for public use.

I'm renaming the key to only now that it is public, which makes it a more expected API name in the Laravel ecosystem. I've depreciated validate but kept support for it in case people have been using it.

<button
    onClick={() => form.validate({
        only: ['name', 'email'],
        onSuccess: () => nextStep(),
    })}
>
    Next Step
</button>

When specifying inputs in the only key, validation will only apply to those inputs. Those keys will also be "touched" to ensure their future validation behaviour is expected, including conditional and interdependent validation rules.

@timacdonald
Copy link
Member Author

Docs: laravel/docs#10058

@timacdonald
Copy link
Member Author

Marking this one as draft while I consider how this API naming could conflict with with Inertia's visit options.

@timacdonald timacdonald marked this pull request as draft December 9, 2024 08:26
@timacdonald
Copy link
Member Author

timacdonald commented Jan 12, 2025

The only config option is never passed to Inertia, so there is no conflict in API naming here. Precognition uses a dedicated Axios client even when used with Inertia.

@timacdonald timacdonald marked this pull request as ready for review January 12, 2025 22:10
@timacdonald timacdonald self-assigned this Jan 12, 2025
@taylorotwell taylorotwell merged commit a044af2 into main Jan 13, 2025
7 checks passed
@taylorotwell taylorotwell deleted the validate-to-only branch January 13, 2025 17:39
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