Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best way to handle JSON arrays of fields? #42

Open
maxcountryman opened this issue Sep 29, 2015 · 1 comment
Open

Best way to handle JSON arrays of fields? #42

maxcountryman opened this issue Sep 29, 2015 · 1 comment

Comments

@maxcountryman
Copy link

I have some JSON that looks like this {'images': [{'name': 'field-1', 'value': 'http://example.com/foo'}, {'name': 'field-2', 'value': 'http://example.com/bar'}, ...]}.

What I'd like to know is what is the best way to validate each of these name-value pairs as though they were fields? In particular, I need them to be optional and valid URLs.

@maxcountryman
Copy link
Author

So I think I was able to solve this like so (assume we've imported the necessary classes here):

class ImageForm(wtforms.Form):
    name = wtforms.TextField('name')
    value = wtforms.TextField('value', [wtforms.validators.Optional(), ...])


class ImagesForm(wtforms.Form):
    images = wtforms.FieldList(wtforms.FormField(ImageForm))

However field errors do not map back to the input names, which are dynamically generated on the front-end, e.g. field-1 where 1 is the ID of an object in our database.

Suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant