Control of the submission for a form
Asked Answered
K

1

-1

I'm still struggling with the same problem about Parsley and semi validation. I have a form with 2 categories of fields

  • "Contact information" (17 fields)
  • "Company information" (5 fields)

The Contact information fields are mandatory. For The "Company information fields", the user must answer only if he has a company. So one radio button named "jform[company]" allows to answer to the question "Do you have a company".

  • If the answer is "No" - I want to apply a Semi validation (just Contact information fields)
  • If the answer is "Yes" - I want to apply a Full validation ( Contact information fields && Company information fields)

Here is my code: (It's highly inspired by the example on the official documentation: http://parsleyjs.org/doc/examples/events.html)

 $('#adminForm').parsley().subscribe('parsley:form:validate', function (formInstance) {
                if (formInstance.isValid('infos'))
                {
                    if ($("input[name='jform[company]']:checked").val() == 1) 
                    {
                        if (formInstance.isValid('comp') )
                        {
                            alert("Parsley - Full validation : OK");
                            return true;
                        }
                        else
                        {
                            alert("Parsley - Full validation : Fail");
                            formInstance.submitEvent.preventDefault();
                        }
                    }
                    else
                    {
                        alert("Parsley - Semi validation : OK");
                        return true;
                    }

                }
                else
                {
                    alert("Parsley - Semi validation : Fail");
                    formInstance.submitEvent.preventDefault();
                }
            });

My problem is that the submission of the form occurs only for the full validation. When I answer No to the question and I fill correctly contact information, the message "Parsley - Semi validation : OK" is displayed but the form is not submitted!

Do you have a possible explanation? Thanks very much

Kotick answered 25/9, 2014 at 17:4 Comment(2)
What was wrong with your last question (#26003130)? If you change the alert to return true, it would submit the form when the fields were correctly filled.Submit
Hello milz. Unfortunately the submission does not work. The code is executed because I reach this point (the message is displayed) but the line return true does not submit the data (nothing happen). In my previous post with the example on jsfiddle I did not test the submission of the form just the conditionnal validation of blocks. I know it is strange... I can't understand why "return true;" does not trigger the submit of the form.Kotick
K
0

It's solved !

As explained by Milz in this post: Validate set of fields only if radio button is checked (conditional validation)

The problem was due to the misunderstanding of the use of "data-parsley-group".

Concerning Parsley.Remote.js, it's true that "formInstance.isValid" does not answer to the question about the validity of remote test. However the Behaviour is okay Because if the remote validation is false the form is Not Submitted and if it is right the form is Submitted!

Kotick answered 26/9, 2014 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.