简体   繁体   中英

Don't want to validate a DateField in wtforms but want to keep the Date format

My form is like this:

forms.py

class FollowUpForm(Form):
assigned_to_user_id = SelectField("Assigned to", coerce=int)
planned_followup_date = DateField("Planned Followup Date", [validators.DataRequired("Date is required")], format="%Y-%b-%d")
actual_followup_date = DateField("Actual Followup Date",format="%Y-%b-%d")
notes = TextAreaField("Notes", [validators.DataRequired("Notes is required")])
status = SelectField("Status", choices = zip(status_categories, status_lebels))

I don't want to validate the actual_followup_date field but want to keep the date format .

My html form is like this

<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" id="FollowUpFormPopUp">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><div aria-hidden="true">&times;</div></button>
                    <h4 class="modal-title" id="myModalLabel"><strong> Followup Form</strong></h4>
                </div>
                <div class="modal-body">
                    <div class="form-wrap">
                        <form id="SubmitFollowUpForm">
                        <div class="form-group" >
                            {{ followup_form.assigned_to_user_id.label }}
                            {{ followup_form.assigned_to_user_id(class_="form-control", type="select") }}
                            <span class="help-block"></span>
                          </div>
                            <div class="form-group " >
                                {{ followup_form.planned_followup_date.label }}
                                {{ followup_form.planned_followup_date(class_="form-control", **{"data-parsley-required":"true", 
                                                                          "data-parsley-required-message":"Planned Followup Date is required",
                                                                          "data-provide":"datepicker",
                                                                       "data-date-format":"yyyy-M-dd"}) }}
                                <span class="help-block"></span>
                            </div>
                            <div class="form-group " >
                                {{ followup_form.actual_followup_date.label }}
                                {{ followup_form.actual_followup_date(class_="form-control", **{"data-parsley-required":"false", 
                                                                          "data-provide":"datepicker",
                                                                       "data-date-format":"yyyy-M-dd"}) }}
                                <span class="help-block"></span>
                            </div>
                           <div class="form-group " >

                                {{ followup_form.notes.label }}
                                {{ followup_form.notes(class_="form-control", rows=10, type="text", **{"data-parsley-required":"true", 
                                                                          "data-parsley-required-message":"Notes is required",
                                                                           }) }}
                                <span class="help-block"></span>
                          </div>
                          <div class="form-group" >
                            {{ followup_form.status.label }}
                            {{ followup_form.status(class_="form-control", type="select") }}
                            <span class="help-block"></span>
                          </div>

                          <button type="submit" class="btn btn-primary btn-block" id="FollowUpSubmitBtn">SUBMIT</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

When I submit the form without providing any value to the Actual_followup_date..its getting error "not a valid date format" . But in this situation I want to store null value to database and not want this span error. you can see the screen-shot of this error.. 错误截图

Please suggest me..thanks..

You need to add a validator of type Optional to your Actual Followup Date field like so:

from wtforms.validators import ..., Optional


...
actual_followup_date = DateField("Actual Followup Date",format="%Y-%b-%d",[validators.Optional()])
...

this will allow you to not enter anything, and will just push the None value to the database.

Create a custom validator.

def customValidatorForFollowupDate(form, field):
    if not form.actual_followup_date.data:
        field.errors[:] = []
        raise StopValidation()

And use this like this way-

class FollowUpForm(Form):
    assigned_to_user_id = SelectField("Assigned to", coerce=int)
    planned_followup_date = DateField("Planned Followup Date", [validators.DataRequired("Date is required")], format="%Y-%b-%d")
    actual_followup_date = DateField("Actual Followup Date",[customValidatorForFollowupDate], format="%Y-%b-%d")
    notes = TextAreaField("Notes", [validators.DataRequired("Notes is required")])
    status = SelectField("Status", choices = zip(status_categories, status_lebels))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM