Bootstrap Validator提交表单

时间:2017-07-18 18:47:51

标签: jquery forms bootstrapvalidator

此时我正在使用bootstrapValidator.min.js 0.5.3来验证表单: https://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.3/js/bootstrapValidator.min.js

现在这适用于联系表单,我将发送邮件事件绑定到。

但现在我想将它用于需要验证表单,发送邮件并提交表单的表单,以便$ _POST获取字段将其写入mysql。

另请注意,我有一个foreach循环,可以在添加更多对象时创建更多表单。 (表单用于申请工作,可以在后端添加工作。)

代码:

我不会发布表单html和PHP,因为我知道它们有效。除非被问到。它是一个非常长的形式,我不想用离线代码混淆它。

验证者:

 jQuery('.form-job').each(function (i, obj) {
        jQuery(this).bootstrapValidator({
            fields: {
                firstname: {
                    validators: {
                        stringLength: {
                            min: 2,
                            message: 'Gelieve 2 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw naam in.'
                        }
                    }
                },
                lastname: {
                    validators: {
                        stringLength: {
                            min: 2,
                            message: 'Gelieve 2 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw achternaam in.'
                        }
                    }
                },
                street: {
                    validators: {
                        stringLength: {
                            min: 5,
                            message: 'Gelieve 5 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw straat in.'
                        }
                    }
                },
                housenumber: {
                    validators: {
                        stringLength: {
                            max: 8,
                            message: 'Vul aub een geldige huisnummer in.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw huisnummer in.'
                        },
                    }
                },
                postal: {
                    validators: {
                        stringLength: {
                            min: 4,
                            max: 8,
                            message: 'Vul aub een geldige postcode in.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw postcode in.'
                        },
                    }
                },
                city: {
                    validators: {
                        stringLength: {
                            min: 2,
                            message: 'Gelieve 2 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw stad in.'
                        }
                    }
                },
                country: {
                    validators: {
                        stringLength: {
                            min: 2,
                            message: 'Gelieve 2 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw land in.'
                        }
                    }
                },
                email: {
                    validators: {
                        notEmpty: {
                            message: 'Vul aub uw e-mailadres in.'
                        },
                        emailAddress: {
                            message: 'Vul aub een geldig e-mailadres in.'
                        }
                    }
                },
                telephone: {
                    validators: {
                        numeric: {
                            message: 'Gelieve alleen nummers te gebruiken.',
                        },
                        stringLength: {
                            min: 9,
                            message: 'Gelieve 9 of meer cijfers in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub een geldig telefoonnummer in.'
                        }
                    }
                },
                motivation: {
                    validators: {
                        stringLength: {
                            min: 10,
                            max: 550,
                            message: 'Vul aub minstens meer dan 10 letters in en minder dan 550.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw vraag in.'
                        }
                    }
                },
                cvurl: {
                    validators: {
                        file: {
                            extension: 'doc,docx,pdf,txt',
                            type: 'application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/txt',
                            maxSize: 5 * 1024 * 1024,   // 5 MB
                            message: 'Het geselecteerd bestand is niet geldig. Dit moet een .doc, .docx, .pdf of een .txt zijn en max 5 MB groot.'
                        }
                    }
                }
            }
        })
            .on('success.form.bv', function (e) {
                jQuery("#button").css('color', 'green');
                jQuery("#button").css('border', '2px solid green');
                jQuery(".alert-success").css('display', 'block');
                jQuery("#button").html('Verstuurd');
                jQuery(this).data('bootstrapValidator').resetForm();

                // Prevent form submission
                e.preventDefault();

                // Get the form instance
                var form = jQuery(e.target);

                // Get the BootstrapValidator instance
                var bv = form.data('bootstrapValidator');

                jQuery.post(my_ajax_object.ajax_url, form.serialize());

            });
    });
请不要介意荷兰人。任何荷兰人都不应该对问题产生任何影响,因为它只是显示的文字。

我也知道它说:

                // Prevent form submission
            e.preventDefault();

但是当我删除它或评论它没有任何变化。

所以我需要它来验证,发送邮件(有效)并提交。

谢谢!

1 个答案:

答案 0 :(得分:1)

所以我做了这个工作。

问题是我试图通过BootstrapValidator通过JS发送邮件。

所以我最终得到的是删除JS中定义任何东西的部分,我还剩下这个:

import tkinter as tk

root = tk.Tk()

label = tk.Label(root, text="Hello, world", background="bisque", cursor="hand1")
label.pack(side="top", fill="x", padx=10, pady=10)

root.mainloop()

然后我只需在提交按钮后用

调用一个函数
jQuery('.form-job').each(function (i, obj) {
        jQuery(this).bootstrapValidator({
            fields: {
                firstname: {
                    validators: {
                        stringLength: {
                            min: 2,
                            message: 'Gelieve 2 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw naam in.'
                        }
                    }
                },
                lastname: {
                    validators: {
                        stringLength: {
                            min: 2,
                            message: 'Gelieve 2 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw achternaam in.'
                        }
                    }
                },
                street: {
                    validators: {
                        stringLength: {
                            min: 5,
                            message: 'Gelieve 5 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw straat in.'
                        }
                    }
                },
                housenumber: {
                    validators: {
                        stringLength: {
                            max: 8,
                            message: 'Vul aub een geldige huisnummer in.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw huisnummer in.'
                        },
                    }
                },
                postal: {
                    validators: {
                        stringLength: {
                            min: 4,
                            max: 8,
                            message: 'Vul aub een geldige postcode in.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw postcode in.'
                        },
                    }
                },
                city: {
                    validators: {
                        stringLength: {
                            min: 2,
                            message: 'Gelieve 2 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw stad in.'
                        }
                    }
                },
                country: {
                    validators: {
                        stringLength: {
                            min: 2,
                            message: 'Gelieve 2 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw land in.'
                        }
                    }
                },
                email: {
                    validators: {
                        notEmpty: {
                            message: 'Vul aub uw e-mailadres in.'
                        },
                        emailAddress: {
                            message: 'Vul aub een geldig e-mailadres in.'
                        }
                    }
                },
                telephone: {
                    validators: {
                        numeric: {
                            message: 'Gelieve alleen nummers te gebruiken.',
                        },
                        stringLength: {
                            min: 9,
                            message: 'Gelieve 9 of meer cijfers in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub een geldig telefoonnummer in.'
                        }
                    }
                },
                motivation: {
                    validators: {
                        stringLength: {
                            min: 10,
                            max: 550,
                            message: 'Vul aub minstens meer dan 10 letters in en minder dan 550.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw vraag in.'
                        }
                    }
                },
                cvurl: {
                    validators: {
                        file: {
                            extension: 'doc,docx,pdf,txt',
                            type: 'application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/txt',
                            maxSize: 5 * 1024 * 1024,   // 5 MB
                            message: 'Het geselecteerd bestand is niet geldig. Dit moet een .doc, .docx, .pdf of een .txt zijn en max 5 MB groot.'
                        }
                    }
                }
            }
        })
    });

(这是一个例子,永远不要使用提交作为提交按钮的名称)。

我调用的函数然后发送邮件并使用数据库部分的查询