Loop through List of strings to get form object attributes values

Do you need to have them as a list of strings? If not, how about something like:

def validator(self):
    text_boxes = [
        self.text_camperfirstname,
        self.text_camperlastname,
        self.text_parentname,
        self.text_parentemail,
        self.text_parentphone,
    ]
    for text_box in text_boxes:
        print(text_box.text)

If you really need them as strings, you can use the getattr function:

def validator(self):
    field_names = [
        "camperfirstname",
        "camperlastname",
        "parentname",
        "parentemail",
        "parentphone",
    ]
    for field_name in field_names:
        text_box = getattr(self, f"text_{field_name}")
        print(text_box.text)
2 Likes