class ChildForm(BaseForm): modifiedon = DateTimeField(validators=[ DataOptional(default=datetime.now), DateRange(min=datetime(1900, 1, 1), max=datetime.now) ], format="%Y-%m-%dT%H:%M:%SZ") name = StringField(validators=[DataRequired()]) known_age = IntegerField(validators=[DataOptional(), NumberRange(min=0, max=99)]) birthdate = DateField(validators=[DataOptional(), DateRange(min=date(1900, 1, 1), max=date.today)], format="%Y-%m-%d")
class PatientForm(BaseForm): record = IntegerField(validators=[DataAuto(Patient), NumberRange(min=1)]) firstname = StringField(validators=[DataRequired(), Length(min=2)]) middlename = StringField(validators=[DataOptional()]) surname = StringField(validators=[DataOptional(), Length(min=2)]) birthdate = DateField(validators=[DataRequired(), DateRange(min=date(1900, 1, 1), max=date.today)], format="%Y-%m-%d") nationality = StringField(validators=[DataRequired(), AnyOf(values=NATIONALITIES)]) occupation = StringField(validators=[DataRequired(), Length(min=3)]) children = FieldList(Field(validators=[DataOptional(), Entity(ChildForm, Child)])) cellphone = StringField(validators=[DataOptional(), Telephone()]) cellphone2 = StringField(validators=[DataOptional(), Telephone()]) telephone = StringField(validators=[DataOptional(), Telephone()]) email = StringField(validators=[DataOptional(), Email()]) province = StringField(validators=[DataOptional(), AnyOf(values=PROVINCES)]) city = StringField(validators=[DataOptional(), Length(min=2)]) district = StringField(validators=[DataOptional()]) advisor = Field(validators=[DataOptional(), Entity(AdvisorForm, Advisor)]) coadvisors = FieldList(Field(validators=[DataOptional(), Entity(AdvisorForm, Advisor)]), validators=[Coadvisors()]) notes = StringField(validators=[DataOptional()])
def test_min_and_max_should_succeed(self): validator = DateRange(min=date(1999, 12, 31), max=date(2000, 1, 2)) validator(self.form, self.field)
def test_max_should_succeed(self): validator = DateRange(max=date(2000, 1, 1)) validator(self.form, self.field) validator = DateRange(max=date(2000, 1, 2)) validator(self.form, self.field)
def test_max_should_fail(self): validator = DateRange(max=date(1999, 12, 31)) with self.assertRaises(ValidationError): validator(self.form, self.field)
def test_min_should_fail(self): validator = DateRange(min=date(2000, 1, 2)) with self.assertRaises(ValidationError): validator(self.form, self.field)
def test_validation_should_succeed_when_no_data(self): self.field.data = None validator = DateRange(min=date(1999, 12, 31), max=date(2000, 1, 2)) validator(self.form, self.field)
def test_new_instance_should_fail(self): with self.assertRaises(ValueError): DateRange()