Validating FlaskForm (Flask-WTForms) SelectField Populated with AJAX

I'm having some trouble submitting a form that includes a SelectField populated by AJAX. Don't mind the letters in front of the variable names, they are there to set their order because the forms are generated automatically:

class CreateEmployeeForm(FlaskForm):
    code = StringField(
        'Employee Code', validators=[DataRequired(), Length(max=16)]
    a__first_name = StringField(
        'First Name', validators=[DataRequired(), Length(max=128)]
    aa__last_name = StringField(
        'Last Name', validators=[DataRequired(), Length(max=128)]
    email = StringField(
        'Email', validators=[DataRequired(), Email(), Length(max=128)]
    start_date = DateField(
        'Start Date', format='%Y-%m-%d', validators=[Optional()]
    phone = StringField(
        'Telephone', validators=[DataRequired(), Length(min=10, max=16)]
    type_id = SelectField(
        'Employee Type', choices=[], coerce=int, 
        validators=[not_blank, v_employee_type], 
        render_kw={'class': 'dropdown_pop', 'id': 'Employee_type:name'}
    z__password = PasswordField(
        'Password', validators=[DataRequired(), Length(min=8)]
    z__password2 = PasswordField(
        'Repeat Password', validators=[DataRequired(), EqualTo('z__password')]
    zzzz__submit = SubmitField(
        'Create Employee', render_kw={'class': 'submit_btn'}

    def validate_code(self, code):
        user = Employee.query.filter_by(
        if user is not None:
            raise ValidationError('This employee id is already taken.')

    def validate_email(self, email):
        user = Employee.query.filter_by(
        if user is not None:
            raise ValidationError('Please use a different email address.')

The issue in question is with type_id which is populated upon rendering with the choices pulled from the DB by a jQuery call to an API.

If I try to submit the form as-is, type_id will not validate and I will get the standard Not a valid choice error.

I read somewhere else that if I set a custom validation function for this field it will override the regular FlaskForm validation. Unfortunately, Flask-WTF uses underscores to detect what needs to be validated and as soon as I add the validate_type_id function the application crashes. I don't think the error is relevant since it is something coming from how FlaskForms is built.

I need to find a way around.

So I tried creating an external validation function and calling it in the validators=[...] within the field inside the form. Didn't work, still got the invalid choice error message.

I'm trying to validate the field by checking that the option selected for that field exists in the DB. Or, is there a way to disable validation for that field with out using validate_fieldName(...) ?

I'm stuck between a rock and a hard place, I cannot change the field name to exclude the underscore because of the way that this is used to create the entry in the DB. From what I've read, I can only use letters, numbers and underscore as variable names in Python, so from that point of view, I'm screwed.

Any ideas?

1 answer