控制表单的提交 [英] Control of the submission for a form

查看:79
本文介绍了控制表单的提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Parsley和Semi验证,我仍然在同一个问题上挣扎. 我有一个包含2个类别的字段的表单

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

  • 联系信息"(17个字段)
  • 公司信息"(5个字段)

联系信息"字段是必填字段. 对于公司信息字段",用户只有在拥有公司的情况下才必须回答. 因此,一个名为"jform [company]"的单选按钮允许回答您有公司吗"这个问题.

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".

  • 如果答案为否"-我想应用一个Semi验证(只是 联系信息字段)
  • 如果答案为是"-我要应用完全验证(联系信息字段和&&公司信息字段)
  • 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)

这是我的代码: (受到官方文档中示例的启发: http://parsleyjs.org/doc/examples/events.html )

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

推荐答案

已解决!

正如Milz在这篇文章中所解释的: 仅在选中单选按钮时验证字段集(条件验证)

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

该问题是由于对"data-parsley-group"的误解所致.

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

关于Parsley.Remote.js,"formInstance.isValid"确实不能回答有关远程测试有效性的问题.但是,该行为还可以,因为如果远程验证为false,则该表单为未提交",如果为正确,则该表单为已提交"!

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!

这篇关于控制表单的提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆