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

Improved form requests #505

Merged
merged 19 commits into from Oct 7, 2020
Merged

Improved form requests #505

merged 19 commits into from Oct 7, 2020

Conversation

bytestream
Copy link
Collaborator

Usage

Change your form request to extends \Proengsoft\JsValidation\Remote\FormRequest. Then use \JsValidator::formRequest(MyRequest::class)

Description

This solves several issues with client side validation of form requests:

How does it work

A single AJAX request validation rule is registered against a dummy input[type=hidden] field which is generated on-the-fly.

When you submit the form an AJAX fires which does the form request validation. The error response is then mapped back to the input fields in the DOM and errors displayed using jquery-validation methods.

Potential pitfalls

Previously inputs were validated as you typed. Now they're only validated when you submit the form. If you change an invalid input, it's no longer immediately validated so the error will go away. When you submit the form, if it's still invalid the error will be reshown.

resources/assets/js/jsvalidation.js Outdated Show resolved Hide resolved
resources/assets/js/jsvalidation.js Outdated Show resolved Hide resolved
resources/assets/js/jsvalidation.js Outdated Show resolved Hide resolved
resources/assets/js/jsvalidation.js Show resolved Hide resolved
resources/assets/js/jsvalidation.js Outdated Show resolved Hide resolved
resources/assets/js/jsvalidation.js Outdated Show resolved Hide resolved
resources/assets/js/jsvalidation.js Outdated Show resolved Hide resolved
resources/assets/js/jsvalidation.js Show resolved Hide resolved
resources/assets/js/jsvalidation.js Show resolved Hide resolved
resources/assets/js/helpers.js Outdated Show resolved Hide resolved
resources/assets/js/helpers.js Outdated Show resolved Hide resolved
resources/assets/js/helpers.js Outdated Show resolved Hide resolved
@yangm97
Copy link

yangm97 commented Oct 4, 2020

Previously inputs were validated as you typed. Now they're only validated when you submit the form.

What about validating inputs when they lose focus?

@bytestream
Copy link
Collaborator Author

Previously inputs were validated as you typed. Now they're only validated when you submit the form.

What about validating inputs when they lose focus?

Could do that but it will fire a lot of AJAX requests in the background

example: element 'brand' has rule 'array' if it fails, the element may be called 'brand[]' so we need to append '[]' to find the correct element
resources/assets/js/helpers.js Outdated Show resolved Hide resolved
resources/assets/js/helpers.js Show resolved Hide resolved
@bytestream bytestream merged commit 8710c4f into master Oct 7, 2020
@bytestream bytestream deleted the form-requests branch October 7, 2020 20:25
@yangm97
Copy link

yangm97 commented Oct 16, 2020

Previously inputs were validated as you typed. Now they're only validated when you submit the form.

What about validating inputs when they lose focus?

Could do that but it will fire a lot of AJAX requests in the background

It shouldn't? In theory there should only be a request when the user switches from one input to the next, which doesn't happen very often.

However, looking at this again, it would seem like the last input would only be validated when clicking the submit, since most users don't change the focus of the last input and instead go straight to the submit button.
So it's very likely the lose focus event would happen almost at the same time as the submit click event, thus sending two validation requests simultaneously... not sure if this is enough of an issue or not.

@bytestream
Copy link
Collaborator Author

The validation is registered against a hidden field that's generated dynamically on page load. Validation will only occur on form submission, as there are no "real" (noop) validation rules registered against the expected fields

@MGParisi
Copy link

MGParisi commented Oct 20, 2021

I'm missing something. Maybe I got the wrong version? This has now just caused the entire thing to stop working. It now refreshes the page and ignores the entire check and falls back on Server side. Also, the form now has a novalidate element.

---- Nevermind ---

I'm abandoning using this project. It just doesn't work very well, and I'm now stuck with new problems and very little motive to continue down this path. Thanks for the work, I'll try using it on another project at another time.

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