Re: Compound validators usage
Chris Lambacher <[email protected]> Sat, 21 Jul 2012 14:43:54 -0400
| Newsgroups | gmane.comp.python.formencode |
|---|---|
| Message-ID | <CAAXXHgJNPf1zXJWV0_YFVWzUoh=nAOwWsnR+KhmVFx9=8fwWTw@mail.gmail.com> |
--===============6246111234645007186==
Content-Type: multipart/alternative; boundary=e89a8f22c34f0c421504c55b65a9
--e89a8f22c34f0c421504c55b65a9
Content-Type: text/plain; charset=UTF-8
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 <[email protected]>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
> FormEncode-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
> https://lists.sourceforge.net/lists/listinfo/formencode-discuss
>
>
--
Christopher Lambacher
[email protected]
--e89a8f22c34f0c421504c55b65a9
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Generally want SimpleFormValidators to be either pre_validators or chained_=
validators see the docs here:<div><a href=3D"http://www.formencode.org/en/l=
atest/Validator.html#compound-validators">http://www.formencode.org/en/late=
st/Validator.html#compound-validators</a>=C2=A0</div>
<div><br></div><div>So I would normally do:</div><div><br></div><div><span =
style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;ba=
ckground-color:rgb(255,255,255)">class ProductConfigurationSchema(</span><s=
pan style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13p=
x;background-color:rgb(255,255,255)">Schema):</span><br style=3D"color:rgb(=
34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(=
255,255,255)">
<span style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size:1=
3px;background-color:rgb(255,255,255)">=C2=A0=C2=A0=C2=A0 filter_extra_fiel=
ds =3D True</span><br style=3D"color:rgb(34,34,34);font-family:arial,sans-s=
erif;font-size:13px;background-color:rgb(255,255,255)">
<span style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size:1=
3px;background-color:rgb(255,255,255)">=C2=A0=C2=A0=C2=A0 allow_extra_field=
s =3D True</span><br style=3D"color:rgb(34,34,34);font-family:arial,sans-se=
rif;font-size:13px;background-color:rgb(255,255,255)">
<span style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size:1=
3px;background-color:rgb(255,255,255)">=C2=A0=C2=A0=C2=A0</span><br style=
=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;backgro=
und-color:rgb(255,255,255)">
<span style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size:1=
3px;background-color:rgb(255,255,255)">=C2=A0=C2=A0=C2=A0=C2=A0 bool_field =
=3D validators.Bool()=C2=A0</span><br style=3D"color:rgb(34,34,34);font-fam=
ily:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<span style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size:1=
3px;background-color:rgb(255,255,255)">=C2=A0=C2=A0=C2=A0=C2=A0 text_field =
=3D UnicodeString(max=3D100)</span></div><div><font color=3D"#222222" face=
=3D"arial, sans-serif">=C2=A0 =C2=A0 =C2=A0</font></div>
<div><font color=3D"#222222" face=3D"arial, sans-serif">=C2=A0 =C2=A0 =C2=
=A0chained_validators =3D [</font>
<span style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size:1=
3px;background-color:rgb(255,255,255)">SimpleFormValidator(validate_</span>=
<span style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size:1=
3px;background-color:rgb(255,255,255)">text)</span>=C2=A0]</div>
<div><br></div><div>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). F=
ollowing 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 bu=
ilt in a RequireIfMissing/RequireIfPresent=C2=A0<a href=3D"http://www.forme=
ncode.org/en/latest/modules/validators.html#formencode.validators.RequireIf=
Missing">http://www.formencode.org/en/latest/modules/validators.html#formen=
code.validators.RequireIfMissing</a>).</div>
<div><br></div><div>One thing you can do with pre_validtors is throw data a=
way 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.</div>
<div><br></div><div>-Chris</div><div><br><div class=3D"gmail_quote">On Sat,=
Jul 21, 2012 at 2:09 PM, Michael Jenny <span dir=3D"ltr"><<a href=3D"ma=
ilto:[email protected]" target=3D"_blank">[email protected]</a>&g=
t;</span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><p dir=3D"ltr">Hi there,</p><p dir=3D"ltr">i=
have a schema validator and want to use another compound validator (Simple=
FormValidator) from within the schema.</p>
<p dir=3D"ltr"></p><p dir=3D"ltr"></p><p dir=3D"ltr">E.g. I do something al=
ong 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'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'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: 'unicode' object has no attribute=
'copy'</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'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>
<br>-----------------------------------------------------------------------=
-------<br>
Live Security Virtual Conference<br>
Exclusive live event will cover all the ways today's security and<br>
threat landscape has changed and how IT managers can respond. Discussions<b=
r>
will include endpoint security, mobile security and the latest in malware<b=
r>
threats. <a href=3D"http://www.accelacomm.com/jaw/sfrnl04242012/114/5012226=
3/" target=3D"_blank">http://www.accelacomm.com/jaw/sfrnl04242012/114/50122=
263/</a><br>_______________________________________________<br>
FormEncode-discuss mailing list<br>
<a href=3D"mailto:FormEncode-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org">FormEncode-disc=
[email protected]</a><br>
<a href=3D"https://lists.sourceforge.net/lists/listinfo/formencode-discuss"=
target=3D"_blank">https://lists.sourceforge.net/lists/listinfo/formencode-=
discuss</a><br>
<br></blockquote></div><br><br clear=3D"all"><div><br></div>-- <br>Christop=
her Lambacher<br><a href=3D"mailto:[email protected]">chris@kateandchr=
is.net</a><br>
</div>
--e89a8f22c34f0c421504c55b65a9--
--===============6246111234645007186==
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/
--===============6246111234645007186==
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
--===============6246111234645007186==--