2

I am trying to use Formalchemy to add a new record to my SQLAlchemy table DataTBL.

fs = FieldSet(DataTBL)
fs.bind(DataTBL, request=requestobject)

if fs.validate():
    fs.sync()
    session.commit()

This gives me a validation error because the DataTable object is still empty...

ValidationError: Cannot validate without binding data

How do I use Formalchemy to start with an empty form that has the DataTBL structure, fill the form and validate/submit it?

1 Answer 1

3

You need a request.POST to use .validate()

Try:

fs = FieldSet(DataTBL)
fs = fs.bind(DataTBL, request=requestobject)

if requestobject.POST and fs.validate():
    fs.sync()
    session.add(fs.model)
    session.commit()
Sign up to request clarification or add additional context in comments.

2 Comments

This fs = FieldSet(DataTBL, request=requestobject) instead of fs = FieldSet(DataTBL) made it work.
oh, yes. .bind() return a new instance so it should be fs = fs.bind(DataTBL, request=requestobject)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.