Merge pull request #546 from dod-ccpo/am-cor-checkbox

am COR checkbox
This commit is contained in:
montana-mil
2019-01-17 11:39:06 -05:00
committed by GitHub
10 changed files with 149 additions and 20 deletions

View File

@@ -12,7 +12,7 @@ from wtforms.fields.html5 import DateField, TelField
from wtforms.widgets import ListWidget, CheckboxInput
from wtforms.validators import Required, Length
from atst.forms.validators import IsNumber, PhoneNumber
from atst.forms.validators import IsNumber, PhoneNumber, RequiredIfNot
from .forms import CacheableForm
from .data import (
@@ -120,17 +120,19 @@ class OversightForm(CacheableForm):
validators=[Required(), Length(min=10), IsNumber()],
)
am_cor = BooleanField(translate("forms.task_order.oversight_am_cor_label"))
cor_first_name = StringField(
translate("forms.task_order.oversight_first_name_label")
)
cor_last_name = StringField(translate("forms.task_order.oversight_last_name_label"))
cor_email = StringField(translate("forms.task_order.oversight_email_label"))
cor_phone_number = TelField(
translate("forms.task_order.oversight_phone_label"), validators=[PhoneNumber()]
translate("forms.task_order.oversight_phone_label"),
validators=[RequiredIfNot("am_cor"), PhoneNumber()],
)
cor_dod_id = StringField(
translate("forms.task_order.oversight_dod_id_label"),
validators=[Required(), Length(min=10), IsNumber()],
validators=[RequiredIfNot("am_cor"), Length(min=10), IsNumber()],
)
so_first_name = StringField(

View File

@@ -1,5 +1,5 @@
import re
from wtforms.validators import ValidationError
from wtforms.validators import ValidationError, StopValidation
import pendulum
from datetime import datetime
from atst.utils.localization import translate
@@ -78,3 +78,28 @@ def ListItemsUnique(message=translate("forms.validators.list_items_unique_messag
raise ValidationError(message)
return _list_items_unique
def RequiredIfNot(other_field_name, message=translate("forms.validators.is_required")):
""" A validator which makes a field required only if another field
has a falsy value
Args:
other_field_name (str): the name of the field we check before
determining if this field is required; if this other field is falsy,
the field will be required
message (str): an optional message to display if the field is
required but hasNone value
"""
def _required_if_not(form, field):
other_field = form._fields.get(other_field_name)
if other_field is None:
raise Exception('no field named "%s" in form' % self.other_field_name)
if not bool(other_field.data):
if field.data is None:
raise ValidationError(message)
else:
raise StopValidation()
return _required_if_not