> We're doing a BFG app with a MongdoDB backend, and the Deform stuff seems
> perfect for us, really enjoying using it so far.
> But I can't figure out how to make a field in a schema optional if it's
> not a String.  (I've done this in Django model schemas where I can use the
> flag "null=True" and blank=True" on a non-string type). My reading of the
> docs says that setting a field's
>   default=...
> it makes a field non-required, and default='' is fine for String types,
> but not so helpful for Integer, Date and other nonStrings.  I've read doc
> mentioning flags:
>   required=False
>   allow_empty=True
> but allow_empty applies only to String and these seem to have no effect
> either.  I'm looking to do something like:
> class StandardFields(colander.MappingSchema):
>     manufacturer                = SchemaNode(String(), default='') # not
> required
>     obsolete_date               = SchemaNode(Date(),    required=False,
> allow_empty=True)
>     length                      = SchemaNode(Float(),   required=False,
> allow_empty=True)
>     units_per_pack              = SchemaNode(Integer(), required=False,
> allow_empty=True)
> but find these don't work: my fields are required to be set.  Supplying a
> default value to indicate a non-required field, e.g.:
>     units_per_pack              = SchemaNode(Integer(), default=0)
> also feels wrong, since this will set the actual value to 0 upon storage
> to the DB.

This is an area that I'm not 100% confident about yet, so I'm glad
you're stressing it a bit.

FTR, this definitely won't work:

   SchemaNode(Integer(), required=False, allow_empty=True)

Because a SchemaNode does nothing with either the ``required`` nor the
``allow_empty`` arguments.  Instead, you probably meant to be passing
these kwargs to the data type constructor ("Integer()").  I just made
a change to Colander that will cause it to throw a TypeError if bogus
keyword arguments are fed to the SchemaNode constructor, hopefully
preventing future confusion about this.

That said, although the ``colander.String`` type accepts an
``allow_empty``, there isn't any data type constructor that accepts a
``required`` argument.  The ``required`` attribute of a SchemaNode is
a computed property, derived from whether or not the node was given a
default value, but types themselves aren't required or not-required,
and neither a SchemaNode nor any particular type (Integer, String,
etc) takes ``required`` as a valid kw argument.

> I am probably missing something in the docs. Perhaps I need a different
> approach for non-required fields, like making the schema a String but
> having a validator ensure they're my desired type if any value is provided
> on the form -- this seems a round-about way to do things.

I don't think you want to change the type.  I think this might be a
deficiency in Colander or Deform, but I'd like to understand a little
better before I make any particular change.  So I'll try to state the
requirements and the current state of affairs to make sure I make the
right change:

  You want to make a field in a form "not-required".  This implies
  that you don't want a little red asterisk next to the field title
  when the form is rendered, and when the form is submitted, you don't
  want the form to be re-rendered with an error message if no value is
  placed into the field representing the value.  Instead, you want the
  form submission to be considered valid, and the data returned from
  the ``validate`` method of the form to contain a placeholder
  (default) value for that particular field.  The placeholder value
  returned by ``validate`` won't necessarily be the same natural type
  as the field data type.  For instance, you might want the
  placeholder value to be ``None``, while the type is an Integer.

Repoze-dev mailing list

Reply via email to