How to get Pylons formencode validator to ignore some fields

1.4k views Asked by At

I have a form that has some input text boxes and well as some select boxes. I have validation working perfectly on the textboxes. I don't care if any of the select boxes are left defualt or not, but everytime I submit the form it goes to a pylons error page saying "Invalid: Please enter value for ..." But I don't want that to happen.

here is my validator function:

class Registration(formencode.Schema):
    allow_extra_fields=True
    first_name = formencode.validators.String(strip=True, not_empty=True)
    last_name = formencode.validators.String(strip=True, not_empty=True)
    address = formencode.validators.String(strip=True, not_empty=True)
    phone = formencode.validators.PhoneNumber(strip=True, not_empty=True)
    email = formencode.validators.Email(resolve_domain=True, not_empty=True)
    messages = {
        'tooLow': "You must be at least 18 in order to register for an event.",
        'tooHigh': "You obviously are not that old.  Please enter your actual age.",
    }
    age = formencode.validators.Int(min=18, max=999, not_empty=True, messages=messages)

I thought with allow_extra_fields = True it would allow for fields in the form that aren't supplied in the function to pass if left blank/default. I have select boxes named heard, frequency, and level that should be ignored.

Any help here would be greatly appreciated.

2

There are 2 answers

0
webjunkie On

I think you should add the following line to it

filter_extra_fields = True

0
Rescommunes On

On your formencode.validators methods:

  • Use not_empty=False if your form is submitting the fields and the values are empty strings. The param not_empty false is how you say: empty is a valid value.
  • If the fields do not exist then use if_missing = None which will set your result to None.