Flask WTForms-如何跳过对不可见div中字段的验证?

时间:2019-02-01 22:38:30

标签: flask flask-wtforms

我在Python Flask中创建了一个WTform,其中有一个交付表单。如果客户的帐单邮寄地址与送货地址不同,那么我将启用div,其中包含帐单邮寄地址的字段。我正在通过jquery隐藏和显示div。 我本来所有的领域中一个Flaskform,但转念一想,如果我把单独的FlaskForm计费领域,手柄form.validate_on_submit()分别为第2形式,然后可能是我可以跳过验证在开票DIV领域。 但是不,它仍在这些字段上执行验证,因此,我无法继续前进。如果用户具有相同的地址,则帐单字段将为空且无法通过验证。 我确定人们一定遇到过这种情况,但是我找不到任何答案。 有人可以指导我吗?

我正在附上代码,以解释自己。

我的Forms.py

class DeliveryForm(FlaskForm):

    choices_billadd = [('1', 'Use delivery address'), ('2', 'Add a billing address')]

    addressline1 = StringField('Address Line 1*',
                           validators=[DataRequired(), Length(min=1, max=100)])
    addressline2 = StringField('Address Line 2',
                           validators=[Length(max=100)])
    city = StringField('City*',
                           validators=[DataRequired(), Length(min=2, max=50)])
    postcode = StringField('Postcode*',
                           validators=[DataRequired(), Length(min=2, max=50)])

    billingaddress = SelectField('Billing Address*', choices = choices_billadd, validators = [DataRequired()])
    submit = SubmitField('Submit')

class BillingForm(FlaskForm):
    bill_addressline1 = StringField('Address Line 1*',
                           validators=[DataRequired(), Length(min=1, max=100)])
    bill_addressline2 = StringField('Address Line 2',
                           validators=[Length(max=100)])
    bill_city = StringField('City*',
                           validators=[DataRequired(), Length(min=2, max=50)])
    bill_postcode = StringField('Postcode*',
                           validators=[DataRequired(), Length(min=2, max=50)])
<form method="POST" action="">
                {{ form.csrf_token(id='login_csrf') }}
                <fieldset class="form-group content-section">

                    <div class="row pt-3">
                        <div class="col-6">
                            {{ form.addressline1.label(class="form-control-label") }}
                            {% if form.addressline1.errors %}
                                {{ form.addressline1(class="form-control is-invalid") }}
                                <div class="invalid-feedback">
                                    {% for error in form.addressline1.errors %}
                                        <span>{{ error }}</span>
                                    {% endfor %}
                                </div>
                            {% else %}     
                                {{ form.addressline1(class="form-control") }}
                            {% endif %}
                        </div>
                        <div class="col-6">
                            {{ form.addressline2.label(class="form-control-label") }}
                            {% if form.addressline2.errors %}
                                {{ form.addressline2(class="form-control is-invalid") }}
                                <div class="invalid-feedback">
                                    {% for error in form.addressline2.errors %}
                                        <span>{{ error }}</span>
                                    {% endfor %}
                                </div>
                            {% else %}     
                                {{ form.addressline2(class="form-control") }}
                            {% endif %}
                        </div>
                    </div>
                    <div class="row pt-3">
                        <div class="col-6">
                            {{ form.city.label(class="form-control-label") }}
                            {% if form.city.errors %}
                                {{ form.city(class="form-control is-invalid") }}
                                <div class="invalid-feedback">
                                    {% for error in form.city.errors %}
                                        <span>{{ error }}</span>
                                    {% endfor %}
                                </div>
                            {% else %}     
                                {{ form.city(class="form-control") }}
                            {% endif %}
                        </div>
                        <div class="col-6">
                            {{ form.postcode.label(class="form-control-label") }}
                            {% if form.postcode.errors %}
                                {{ form.postcode(class="form-control is-invalid") }}
                                <div class="invalid-feedback">
                                    {% for error in form.postcode.errors %}
                                        <span>{{ error }}</span>
                                    {% endfor %}
                                </div>
                            {% else %}     
                                {{ form.postcode(class="form-control") }}
                            {% endif %}
                        </div>
                    </div>

                    <div class="row pt-3">
                        <div class="col-12">
                            {{ form.billingaddress.label(class="form-control-label") }}
                            {{ form.billingaddress(class="form-control") }}
                        </div>  
                    </div>
                    <div id="div_billingaddress" class="">
                        <div class="row pt-3">
                            <div class="col-6">
                                {{ form_bill.bill_addressline1.label(class="form-control-label") }}
                                {% if form_bill.bill_addressline1.errors %}
                                    {{ form_bill.bill_addressline1(class="form-control is-invalid") }}
                                    <div class="invalid-feedback">
                                        {% for error in form_bill.bill_addressline1.errors %}
                                            <span>{{ error }}</span>
                                        {% endfor %}
                                    </div>
                                {% else %}     
                                    {{ form_bill.bill_addressline1(class="form-control") }}
                                {% endif %}
                            </div>
                            <div class="col-6">
                                {{ form_bill.bill_addressline2.label(class="form-control-label") }}
                                {% if form_bill.bill_addressline2.errors %}
                                    {{ form_bill.bill_addressline2(class="form-control is-invalid") }}
                                    <div class="invalid-feedback">
                                        {% for error in form_bill.bill_addressline2.errors %}
                                            <span>{{ error }}</span>
                                        {% endfor %}
                                    </div>
                                {% else %}     
                                    {{ form_bill.bill_addressline2(class="form-control") }}
                                {% endif %}
                            </div>
                        </div>
                        <div class="row pt-3">
                            <div class="col-6">
                                {{ form_bill.bill_city.label(class="form-control-label") }}
                                {% if form_bill.bill_city.errors %}
                                    {{ form_bill.bill_city(class="form-control is-invalid") }}
                                    <div class="invalid-feedback">
                                        {% for error in form_bill.bill_city.errors %}
                                            <span>{{ error }}</span>
                                        {% endfor %}
                                    </div>
                                {% else %}     
                                    {{ form_bill.bill_city(class="form-control") }}
                                {% endif %}
                            </div>
                            <div class="col-6">
                                {{ form_bill.bill_postcode.label(class="form-control-label") }}
                                {% if form_bill.bill_postcode.errors %}
                                    {{ form_bill.bill_postcode(class="form-control is-invalid") }}
                                    <div class="invalid-feedback">
                                        {% for error in form_bill.bill_postcode.errors %}
                                            <span>{{ error }}</span>
                                        {% endfor %}
                                    </div>
                                {% else %}     
                                    {{ form_bill.bill_postcode(class="form-control") }}
                                {% endif %}
                            </div>
                        </div>
                    </div>
                </fieldset>
                <div class="row py-4">
                    <div class="col-12">
                        {{ form.submit(class="btn btn-success btn-block") }}
                    </div>
                </div>
            </form>

0 个答案:

没有答案