Compound validators usage

Michael Jenny <[email protected]> Sat, 21 Jul 2012 20:09:08 +0200
Newsgroups gmane.comp.python.formencode
Message-ID <CAPJpKVAZorWas8bC2dF-vMLUo2=t4sb65gTUhSsxbQ6fVLGaQA@mail.gmail.com>
--===============2422308321729220411==
Content-Type: multipart/alternative; boundary=f46d04430404e784cf04c55ae9b4

--f46d04430404e784cf04c55ae9b4
Content-Type: text/plain; charset=UTF-8

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.

--f46d04430404e784cf04c55ae9b4
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<p dir=3D"ltr">Hi there,</p><p dir=3D"ltr">i have a schema validator and wa=
nt to use another compound validator (SimpleFormValidator) from within the =
schema.</p><p dir=3D"ltr"></p><p dir=3D"ltr"></p><p dir=3D"ltr">E.g. I do s=
omething along the lines:</p>

<p dir=3D"ltr"><br></p><p dir=3D"ltr">def validate_text(value_dict, state, =
validator):</p><p dir=3D"ltr">=C2=A0=C2=A0=C2=A0=C2=A0 # validation of text=
_field goes here. check value_dict for bool_field&#39;s value to determine =
what to do next. <br>
</p>
<p dir=3D"ltr"><br></p><p dir=3D"ltr">class ProductConfigurationSchema(Sche=
ma):<br>=C2=A0=C2=A0=C2=A0 filter_extra_fields =3D True<br>=C2=A0=C2=A0=C2=
=A0 allow_extra_fields =3D True<br>=C2=A0=C2=A0 <br>=C2=A0=C2=A0=C2=A0=C2=
=A0 bool_field =3D validators.Bool() <br>=C2=A0=C2=A0=C2=A0=C2=A0 text_fiel=
d =3D SimpleFormValidator(validate_text)</p>

<p dir=3D"ltr"><br></p><p dir=3D"ltr"></p><p dir=3D"ltr">This does not work=
 as the schema does not expect that its fields are compound validators. It =
doesn&#39;t pass the value_dict to the SimpleFormValidator but rather a sin=
gle value. This results in the following errror in SimpleFormValidators to_=
python method:<br>

</p><p dir=3D"ltr"></p><p dir=3D"ltr">=C2=A0=C2=A0=C2=A0 value_dict =3D val=
ue_dict.copy()<br>AttributeError: &#39;unicode&#39; object has no attribute=
 &#39;copy&#39;</p><p dir=3D"ltr"></p><p dir=3D"ltr"><br></p><p dir=3D"ltr"=
>I followed that route because I want to achieve the following:</p>

<p dir=3D"ltr">The visibility of the text_field depends on the bool_field. =
Therefore I&#39;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.</p>

<p dir=3D"ltr"><br>Any tip how to tackle this?<br></p><p dir=3D"ltr"></p><p=
 dir=3D"ltr"><br></p><p dir=3D"ltr">Cheers, prinz.<br></p><p dir=3D"ltr"></=
p><p dir=3D"ltr"></p>

--f46d04430404e784cf04c55ae9b4--


--===============2422308321729220411==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

------------------------------------------------------------------------------
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/
--===============2422308321729220411==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
FormEncode-discuss mailing list
FormEncode-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/formencode-discuss

--===============2422308321729220411==--