Control of the submission for a form

2019-09-22 03:35发布

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

2条回答
我想做一个坏孩纸
2楼-- · 2019-09-22 04:07

OK I get it! I understand why it does not work properly but I'm not able to correct it for the moment...

In fact I use also Parsley.Remote.js for 2 tests:

  • Control if the email is available (not already taken)
  • Captcha Control (value correct?)

Parsley.Remote.js seems prevent the submission whatever is the result of "formInstance.isValid() ". With other words: formInstance.isValid() does not valid the remote test. I will continue to investigate in order to find a solution

查看更多
smile是对你的礼貌
3楼-- · 2019-09-22 04:20

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!

查看更多
登录 后发表回答