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

Submit button disabled until all form fields are valid. #1996

Closed
harkinj opened this issue Apr 19, 2017 · 8 comments
Closed

Submit button disabled until all form fields are valid. #1996

harkinj opened this issue Apr 19, 2017 · 8 comments
Labels
stale Used to mark stale issues

Comments

@harkinj
Copy link

harkinj commented Apr 19, 2017

Hi,
Great plug-in.
Is there a way of initially setting the Form Submit button to disabled and it is only enabled when all the forms fields have been successfully validated and hence are ready to be submitted?
I've looked through help and issues and have found nothing to do this and I'm guessing it would be a common use case.
Thanks for your time.
John

@pashkes
Copy link

pashkes commented Jul 2, 2017

Found a solution to this problem?

@Anima-t3d
Copy link

Same request...

@Anima-t3d
Copy link

@harkinj @pashkes The closest I found so far is this: https://stackoverflow.com/a/27050178/3163075

$('input').on('blur keyup', function() {
    if ($("#myform").valid()) {
        $('#submit').prop('disabled', false);  
    } else {
        $('#submit').prop('disabled', 'disabled');
    }
});

This validation triggers all form fields on the page once you 'blur' from one input.
You can attach .valid() to the individual fields within the onfocusout handler. However, for activating your submit button, you cannot invoke .valid() to return a boolean without also invoking the error messages... .valid() does both at the same time. No method is provided that can tell you if the form is valid without also activating validation on the form.

If you write your own custom function for this, then you wouldn't need the plugin because you'd essentially be writing client-side validation from scratch. Maybe you can restrict the handler above to only fire when no fields are left blank... it's not perfect.

The developer has not provided any event that fires on a valid form. However, the blur and keyup events normally fire the validation test, so you can also use these to trigger a validation test and activate the button.

I think it basically comes down to someone like @staabm who needs to implement this feature.

@Anima-t3d
Copy link

The code below is what I ended up with so far:

$('#formId').on('blur keyup change', 'input', function(event) {
  validateForm('#formId');
});

function validateForm(id) {
  var valid = $(id).validate().checkForm();
    if (valid) {
      $('.form-save').prop('disabled', false);
        $('.form-save').removeClass('isDisabled');
    } else {
      $('.form-save').prop('disabled', 'disabled');
      $('.form-save').addClass('isDisabled');
    }
}

// Run once, so subsequent input will be show error message upon validation
validateForm('#formId');

It uses checkForm() instead of the form() and my disable button has the classform-save

@stale
Copy link

stale bot commented Jun 5, 2018

This issue/proposal has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and one of the maintainers will (try!) to follow up.
Thank you for contributing :)

@stale stale bot added the stale Used to mark stale issues label Jun 5, 2018
@stale stale bot closed this as completed Jun 12, 2018
@it4need
Copy link

it4need commented Aug 9, 2018

Does anyone know a ability to disable the button while the form is invalid? We check the fields instantly after focus out of the specific field.

@vhugogarcia
Copy link

It appears that there is no way to do such thing @it4need I ended also doing a custom code with the keyup event on each input to validate that.

@mbarmettler
Copy link

eileenmcnaughton added a commit to eileenmcnaughton/nz.co.fuzion.omnipaymultiprocessor that referenced this issue Jul 22, 2019
This has the effect of disabling the paypal buttons until basic validate passes. The goal
here is to prevent the scenario where someone clicks on the paypal button, authorizes the payment
and then has to re-authorize due to the form submission not being accepted. We REALLY don't want
to take their authorization until the form is correct. Note that there is
an api we could use (ContributionPage.validate ['action' => 'create', 'id' => x]

I couldn't find any evidence of being able to listen to when a form is valid - posts like this
jquery-validation/jquery-validation#1996
suggest the method I used.

The easiest way to test this is by submitting the form as an anonymous user without
entering an email - you should not be able to launch the paypal form and there should be red writing next to
the field.

Unfortunately without a core patch you will ALSO get an alert. I am proposing a core patch along side this
which will suppress it in core. Really I think it would be best to put the message down by the paypal button
(possibly hiding it) but because of the need to call it at the start it needs to be less nasty.
eileenmcnaughton added a commit to eileenmcnaughton/nz.co.fuzion.omnipaymultiprocessor that referenced this issue Jul 22, 2019
This has the effect of disabling the paypal buttons until basic validate passes. The goal
here is to prevent the scenario where someone clicks on the paypal button, authorizes the payment
and then has to re-authorize due to the form submission not being accepted. We REALLY don't want
to take their authorization until the form is correct. Note that there is
an api we could use (ContributionPage.validate ['action' => 'create', 'id' => x]

This attempt #98 was against
the previous version of paypal smartbuttons but it demonstrates the api

I couldn't find any evidence of being able to listen to when a form is valid - posts like this
jquery-validation/jquery-validation#1996
suggest the method I used.

The easiest way to test this is by submitting the form as an anonymous user without
entering an email - you should not be able to launch the paypal form and there should be red writing next to
the field.

Paypal documentation on it

https://developer.paypal.com/docs/checkout/integration-features/validation/?mark=validat#synchronous-validation

Note the async mode is BAD - it launches the modal & does the check while loading. Avoid. Perhaps we could only render
the paypal button when valid - like they suggest - but then switching back & forth with valid & invalid gets hard.

Am on the fence about hiding
Unfortunately without a core patch you will ALSO get an alert. I am proposing a core patch along side this
which will suppress it in core. Really I think it would be best to put the message down by the paypal button
(possibly hiding it) but because of the need to call it at the start it needs to be less nasty.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Used to mark stale issues
Projects
None yet
Development

No branches or pull requests

6 participants