Improve validation for Person field of User form

otherwise if user enters e.g. "John Doe" but does *not* select an
autocomplete result, then "John Doe" will be submitted as-is to the
server, which then tried to write that directly to
``users.person_uuid`` column in the DB, resulting in error
This commit is contained in:
Lance Edgar 2021-10-28 18:55:28 -05:00
parent 2d87ce5c29
commit 4dfc29768c

View file

@ -164,6 +164,17 @@ class UserView(PrincipalMasterView):
if query.count():
raise colander.Invalid(node, "Username must be unique")
def valid_person(self, node, value):
"""
Make sure ``value`` corresponds to an existing
``Person.uuid``.
"""
if value:
model = self.model
person = self.Session.query(model.Person).get(value)
if not person:
raise colander.Invalid(node, "Person not found (you must *select* a record)")
def configure_form(self, f):
super(UserView, self).configure_form(f)
user = f.model_instance
@ -188,6 +199,7 @@ class UserView(PrincipalMasterView):
people_url = self.request.route_url('people.autocomplete')
f.set_widget('person_uuid', forms.widgets.JQueryAutocompleteWidget(
field_display=person_display, service_url=people_url))
f.set_validator('person_uuid', self.valid_person)
f.set_label('person_uuid', "Person")
# person name(s)