Generally want SimpleFormValidators to be either pre_validators or
chained_validators see the docs here:
http://www.formencode.org/en/latest/Validator.html#compound-validators
So I would normally do:
class ProductConfigurationSchema(Schema):
filter_extra_fields = True
allow_extra_fields = True
bool_field = validators.Bool()
text_field = UnicodeString(max=100)
chained_validators = [ SimpleFormValidator(validate_text) ]
In this case, the main validator gets the bare minimum validation (I must
be a unicode string and may have a max length of 100). Following on that
you do form level validation in the chained_validators so I would expect
validate_text do do something like make text_field required if bool_field
is true (Note that there is a good model for building that built in a
RequireIfMissing/RequireIfPresent
http://www.formencode.org/en/latest/modules/validators.html#formencode.validators.RequireIfMissing
).
One thing you can do with pre_validtors is throw data away if a value is
set. So if your Bool() field was for deletion, you could remove the other
data from the form so that incorrect data does not cause a validation error.
-Chris
On Sat, Jul 21, 2012 at 2:09 PM, Michael Jenny <pri...@gm...>wrote:
> Hi there,
>
> i have a schema validator and want to use another compound validator
> (SimpleFormValidator) from within the schema.
>
> E.g. I do something along the lines:
>
>
> def validate_text(value_dict, state, validator):
>
> # validation of text_field goes here. check value_dict for
> bool_field's value to determine what to do next.
>
>
> class ProductConfigurationSchema(Schema):
> filter_extra_fields = True
> allow_extra_fields = True
>
> bool_field = validators.Bool()
> text_field = SimpleFormValidator(validate_text)
>
>
> This does not work as the schema does not expect that its fields are
> compound validators. It doesn't pass the value_dict to the
> SimpleFormValidator but rather a single value. This results in the
> following errror in SimpleFormValidators to_python method:
>
> value_dict = value_dict.copy()
> AttributeError: 'unicode' object has no attribute 'copy'
>
>
> I followed that route because I want to achieve the following:
>
> The visibility of the text_field depends on the bool_field. Therefore I'd
> thought I write a custom method that checks the state of the bool when
> validating the text_field. If it is set, I would perform the actual
> validation. If the bool is not set, the text_field is irrelevant and
> validating would essentially be a no-op.
>
>
> Any tip how to tackle this?
>
>
> Cheers, prinz.
>
>
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond. Discussions
> will include endpoint security, mobile security and the latest in malware
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> FormEncode-discuss mailing list
> For...@li...
> https://lists.sourceforge.net/lists/listinfo/formencode-discuss
>
>
--
Christopher Lambacher
ch...@ka...
|