我的表格是这样的:
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))
我不想验证actual_followup_date字段,但想保留日期格式。
我的html表单就像这样
<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">×</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>
当我提交表单而不向Actual_followup_date提供任何值时,获取错误“不是有效的日期格式”。但在这种情况下,我想将null值存储到数据库,不希望这个span错误。你可以看到这个错误的屏幕截图..
请建议我......谢谢..
答案 0 :(得分:1)
您需要在“实际跟进日期”字段中添加“可选”类型的验证器,如下所示:
from wtforms.validators import ..., Optional
...
actual_followup_date = DateField("Actual Followup Date",format="%Y-%b-%d",[validators.Optional()])
...
这将允许您不输入任何内容,只会将None值推送到数据库。
答案 1 :(得分:0)
创建自定义验证程序。
def customValidatorForFollowupDate(form, field):
if not form.actual_followup_date.data:
field.errors[:] = []
raise StopValidation()
并以这种方式使用 -
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))