laravel / precognition

Anticipate the outcome of a future HTTP request.
https://laravel.com/docs/precognition
MIT License
136 stars 35 forks source link

Validate more than one field #47

Closed makroxyz closed 1 year ago

makroxyz commented 1 year ago

I'm using precognition form in a wizard. Every step has some properties of that form. I'd like, when press next, to validate only fields in the current step.

Is there a way to do it?

driesvints commented 1 year ago

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.

makroxyz commented 1 year ago

@driesvints can I open a feature request for form.validate()? I think it could be useful to let pass a string or an array of strings with field(s) to validate

driesvints commented 1 year ago

You can always attempt a PR to see if Taylor would accept it 👍

timacdonald commented 1 year ago

@makroxyz you can use the form.touch and form.touch helpers to modify what is supposed to be validated.

For example, when attempting to go to the next page you might do the following...

form.touch(['name', 'email', 'address']).validate()

This will validate all the fields, even if they have not been interacted with by the user.

If you want to exclude previously validated inputs you could use the touch helper...

form.touch(['name', 'email', 'address']).validate()
makroxyz commented 1 year ago

@timacdonald thank you very much!!!

mreduar commented 1 year ago

@makroxyz you can use the form.touch and form.touch helpers to modify what is supposed to be validated.

For example, when attempting to go to the next page you might do the following...

form.touch(['name', 'email', 'address']).validate()

This will validate all the fields, even if they have not been interacted with by the user.

If you want to exclude previously validated inputs you could use the touch helper...

form.touch(['name', 'email', 'address']).validate()

Thank you! That was one of the things I wanted since I saw Laracon.

gbaggaley commented 1 year ago

form.touch has solved one issue for me, but how would I then run a callback function to wait until the form has finished validating the given fields?

For example my code is like this, but I need to wait until the validation has finished before emitting the event.

function nextStep() {
    form.touch(["name", "email", "password", "password_validation"]).validate();

    emit("next");
}
mreduar commented 1 year ago

form.touch has solved one issue for me, but how would I then run a callback function to wait until the form has finished validating the given fields?

For example my code is like this, but I need to wait until the validation has finished before emitting the event.

function nextStep() {
    form.touch(["name", "email", "password", "password_validation"]).validate();

    emit("next");
}

It would be great to know this, I also have this problem.

timacdonald commented 10 months ago

We will be adding the ability to detect individual validation responses. Follow along in https://github.com/laravel/precognition/issues/59

timacdonald commented 3 months ago

Opened for review: https://github.com/laravel/precognition/pull/69

timacdonald commented 2 months ago

Merged and tagged.

<TextInput
    v-model="form.name"
    @change="form.validate('name', {
        onSuccess: () => /* ... */,
        onValidationError: () => /* .. */,
    })"
/>