Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

javascript - Control of the submission for a form

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...