Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make file upload facultative with Deform and Colander?

I would like to render a form containing a sequence of files, representing different images of a product. Providing files should be facultative, so the form should validate even in the absence of files. How can I do this ?

Here is the colander schema I use:

    import colander
    import deform
    from deform import Form
    from deform import ValidationFailure
    from deform.interfaces import FileUploadTempStore 


    tmpstore = FileUploadTempStore()

    class Image(colander.Schema):
        image = colander.SchemaNode(
            deform.FileData(),
            widget=deform.widget.FileUploadWidget(tmpstore)
            ) 

    class Images(colander.SequenceSchema):
        images = Image()

    class ProductSchema(colander.Schema):
        completename = colander.SchemaNode(colander.String(), title="Complete Name")

        description = colander.SchemaNode(colander.String(), 
                                 widget = deform.widget.TextAreaWidget())

        images = Images()


    schema = ProductSchema()
    form = Form(schema, buttons=("submit", ))

I tried to add a 'missing' argument like:

image = colander.SchemaNode(
        deform.FileData(),
        missing = ???
        widget=deform.widget.FileUploadWidget(tmpstore)
        ) 

I think I get something functional when

missing={'filename': None, 'uid':None}

But I'm really not sure it's the correct way to do it...

Thanks !

like image 419
ascobol Avatar asked Jul 03 '11 14:07

ascobol


1 Answers

You might try "missing = colander.null".

like image 181
Chris McDonough Avatar answered Sep 19 '22 21:09

Chris McDonough