Discussion:
[FE-discuss] Partial validation results from an invalid input?
Daniel Lepage
2011-12-26 22:12:39 UTC
Permalink
Hi everyone,

Is there a way to get the python values for successful fields in a
Schema when other fields have failed?

For example, suppose I have the following schema and input value:

sch = Schema(fields=dict(x=Int(), y=DateConverter(), z=Int()))
v = dict(x='12', y='1/16/1973', z='foo')

I'd like to validate v against sch in such a way that I get not only
the error message from z, but also the values 12 and datetime(1973, 1,
16) from x and y, respectively.

Is there a way to do this?

None of my use cases rely on chained_validators, so all I need are the
values computed by the fields of the schema.

Thanks,
Dan Lepage
Chris Lambacher
2011-12-26 22:41:35 UTC
Permalink
Hi Dan,

There is no official and nice way of doing that and without knowing
why you want to do that I would guess "you are doing it wrong". There
are some hackish ways that you might be able to get the value but they
are likely to be error prone.

Can you elaborate on why you want to do it that way? Do you want to
display the error? Do you want to ignore the error?

-Chris
Post by Daniel Lepage
Hi everyone,
Is there a way to get the python values for successful fields in a
Schema when other fields have failed?
sch = Schema(fields=dict(x=Int(), y=DateConverter(), z=Int()))
v = dict(x='12', y='1/16/1973', z='foo')
I'd like to validate v against sch in such a way that I get not only
the error message from z, but also the values 12 and datetime(1973, 1,
16) from x and y, respectively.
Is there a way to do this?
None of my use cases rely on chained_validators, so all I need are the
values computed by the fields of the schema.
Thanks,
Dan Lepage
------------------------------------------------------------------------------
Write once. Port to many.
Get the SDK and tools to simplify cross-platform app development. Create
new or port existing apps to sell to consumers worldwide. Explore the
Intel AppUpSM program developer opportunity. appdeveloper.intel.com/join
http://p.sf.net/sfu/intel-appdev
_______________________________________________
FormEncode-discuss mailing list
https://lists.sourceforge.net/lists/listinfo/formencode-discuss
--
Christopher Lambacher
***@kateandchris.net
Daniel Lepage
2011-12-26 22:55:42 UTC
Permalink
Hi Chris,

I have a web form that can be edited and saved multiple times. When
the user inputs data, all valid fields are saved and all invalid
fields display error messages.

So I need to show the error messages for the invalid fields AND get
the python values of the valid fields.

Right now my hack is a subclass of Schema that overrides _to_python
with a function that is char-for-char identical except for a single
line, where instead of raising Invalid I raise a custom subclass
(PartialInvalid) that also contains all the valid values.

This works well enough, but it's inelegant, so I was hoping to find a
better solution.

Thanks,
Dan
Post by Chris Lambacher
Hi Dan,
There is no official and nice way of doing that and without knowing
why you want to do that I would guess "you are doing it wrong". There
are some hackish ways that you might be able to get the value but they
are likely to be error prone.
Can you elaborate on why you want to do it that way? Do you want to
display the error? Do you want to ignore the error?
-Chris
Post by Daniel Lepage
Hi everyone,
Is there a way to get the python values for successful fields in a
Schema when other fields have failed?
sch = Schema(fields=dict(x=Int(), y=DateConverter(), z=Int()))
v = dict(x='12', y='1/16/1973', z='foo')
I'd like to validate v against sch in such a way that I get not only
the error message from z, but also the values 12 and datetime(1973, 1,
16) from x and y, respectively.
Is there a way to do this?
None of my use cases rely on chained_validators, so all I need are the
values computed by the fields of the schema.
Thanks,
Dan Lepage
------------------------------------------------------------------------------
Write once. Port to many.
Get the SDK and tools to simplify cross-platform app development. Create
new or port existing apps to sell to consumers worldwide. Explore the
Intel AppUpSM program developer opportunity. appdeveloper.intel.com/join
http://p.sf.net/sfu/intel-appdev
_______________________________________________
FormEncode-discuss mailing list
https://lists.sourceforge.net/lists/listinfo/formencode-discuss
--
Christopher Lambacher
Chris Lambacher
2011-12-26 23:02:03 UTC
Permalink
Hi Dan,

Use the raw post values to render the page. You shouldn't need
validated values to do so. If you need to get related data using some
of the values, you can check if those values specifically caused
errors and do a more direct conversion if not.

If you tell me the framework you are using I might be able to provide
you with some more specific tips.

-Chris
Post by Daniel Lepage
Hi Chris,
I have a web form that can be edited and saved multiple times. When
the user inputs data, all valid fields are saved and all invalid
fields display error messages.
So I need to show the error messages for the invalid fields AND get
the python values of the valid fields.
Right now my hack is a subclass of Schema that overrides _to_python
with a function that is char-for-char identical except for a single
line, where instead of raising Invalid I raise a custom subclass
(PartialInvalid) that also contains all the valid values.
This works well enough, but it's inelegant, so I was hoping to find a
better solution.
Thanks,
Dan
Post by Chris Lambacher
Hi Dan,
There is no official and nice way of doing that and without knowing
why you want to do that I would guess "you are doing it wrong". There
are some hackish ways that you might be able to get the value but they
are likely to be error prone.
Can you elaborate on why you want to do it that way? Do you want to
display the error? Do you want to ignore the error?
-Chris
Post by Daniel Lepage
Hi everyone,
Is there a way to get the python values for successful fields in a
Schema when other fields have failed?
sch = Schema(fields=dict(x=Int(), y=DateConverter(), z=Int()))
v = dict(x='12', y='1/16/1973', z='foo')
I'd like to validate v against sch in such a way that I get not only
the error message from z, but also the values 12 and datetime(1973, 1,
16) from x and y, respectively.
Is there a way to do this?
None of my use cases rely on chained_validators, so all I need are the
values computed by the fields of the schema.
Thanks,
Dan Lepage
------------------------------------------------------------------------------
Write once. Port to many.
Get the SDK and tools to simplify cross-platform app development. Create
new or port existing apps to sell to consumers worldwide. Explore the
Intel AppUpSM program developer opportunity. appdeveloper.intel.com/join
http://p.sf.net/sfu/intel-appdev
_______________________________________________
FormEncode-discuss mailing list
https://lists.sourceforge.net/lists/listinfo/formencode-discuss
--
Christopher Lambacher
------------------------------------------------------------------------------
Write once. Port to many.
Get the SDK and tools to simplify cross-platform app development. Create
new or port existing apps to sell to consumers worldwide. Explore the
Intel AppUpSM program developer opportunity. appdeveloper.intel.com/join
http://p.sf.net/sfu/intel-appdev
_______________________________________________
FormEncode-discuss mailing list
https://lists.sourceforge.net/lists/listinfo/formencode-discuss
--
Christopher Lambacher
***@kateandchris.net
Daniel Lepage
2011-12-26 23:40:16 UTC
Permalink
Hi Chris,

This is done through AJAX. I don't have the "enter values, then hit
save" workflow - instead, the user can change the values in these form
fields at any time and the results are immediately sent to the server
and, if valid, saved.

The values are used by other parts of the system, so I can't just
store the POSTed strings - I need the actual converted values for all
the valid POSTed values, even if there are also invalid values.

The framework backing this is Flask at a high leve and ToscaWidgets 2
at a lower level, but due to a separate data API the validation is
ToscaWidgets-independent.

Thanks,
Dan
Post by Chris Lambacher
Hi Dan,
Use the raw post values to render the page. You shouldn't need
validated values to do so. If you need to get related data using some
of the values, you can check if those values specifically caused
errors and do a more direct conversion if not.
If you tell me the framework you are using I might be able to provide
you with some more specific tips.
-Chris
Post by Daniel Lepage
Hi Chris,
I have a web form that can be edited and saved multiple times. When
the user inputs data, all valid fields are saved and all invalid
fields display error messages.
So I need to show the error messages for the invalid fields AND get
the python values of the valid fields.
Right now my hack is a subclass of Schema that overrides _to_python
with a function that is char-for-char identical except for a single
line, where instead of raising Invalid I raise a custom subclass
(PartialInvalid) that also contains all the valid values.
This works well enough, but it's inelegant, so I was hoping to find a
better solution.
Thanks,
Dan
Post by Chris Lambacher
Hi Dan,
There is no official and nice way of doing that and without knowing
why you want to do that I would guess "you are doing it wrong". There
are some hackish ways that you might be able to get the value but they
are likely to be error prone.
Can you elaborate on why you want to do it that way? Do you want to
display the error? Do you want to ignore the error?
-Chris
Post by Daniel Lepage
Hi everyone,
Is there a way to get the python values for successful fields in a
Schema when other fields have failed?
sch = Schema(fields=dict(x=Int(), y=DateConverter(), z=Int()))
v = dict(x='12', y='1/16/1973', z='foo')
I'd like to validate v against sch in such a way that I get not only
the error message from z, but also the values 12 and datetime(1973, 1,
16) from x and y, respectively.
Is there a way to do this?
None of my use cases rely on chained_validators, so all I need are the
values computed by the fields of the schema.
Thanks,
Dan Lepage
------------------------------------------------------------------------------
Write once. Port to many.
Get the SDK and tools to simplify cross-platform app development. Create
new or port existing apps to sell to consumers worldwide. Explore the
Intel AppUpSM program developer opportunity. appdeveloper.intel.com/join
http://p.sf.net/sfu/intel-appdev
_______________________________________________
FormEncode-discuss mailing list
https://lists.sourceforge.net/lists/listinfo/formencode-discuss
--
Christopher Lambacher
------------------------------------------------------------------------------
Write once. Port to many.
Get the SDK and tools to simplify cross-platform app development. Create
new or port existing apps to sell to consumers worldwide. Explore the
Intel AppUpSM program developer opportunity. appdeveloper.intel.com/join
http://p.sf.net/sfu/intel-appdev
_______________________________________________
FormEncode-discuss mailing list
https://lists.sourceforge.net/lists/listinfo/formencode-discuss
--
Christopher Lambacher
Chris Lambacher
2011-12-27 00:53:03 UTC
Permalink
Post by Daniel Lepage
Hi Chris,
This is done through AJAX. I don't have the "enter values, then hit
save" workflow - instead, the user can change the values in these form
fields at any time and the results are immediately sent to the server
and, if valid, saved.
It is not common to save *some* of the data (i.e. the parts that
validated) rather than *all* the data (i.e. on completely valid). I
think as a user, in most cases, I would find this surprising
behaviour.
Post by Daniel Lepage
The values are used by other parts of the system, so I can't just
store the POSTed strings - I need the actual converted values for all
the valid POSTed values, even if there are also invalid values.
You certainly don't want to use the raw posted values in general for
anything other than re-generating forms. ToscaWidgets *should* be
capable of regenerating forms on error (I haven't looked at
ToscaWidgets in a long time and not very closely either).

If you are doing an AJAX Post, then you should also be able to return
success or failure back to the form (including information about which
fields failed) and leave the content of the page untouched unless you
see that the validation completed because the server gave you a
successful result.

If you *really* want to collect partial success and the errors, you
should probably eschew Schemas and write a function which takes a dict
of validators and returns the errors and the valid values. This is
fairly trivial to write:

def validate_things(validators, form_values):
valid = {}
errors = {}
for key, validator in validators.iteritems():
try:
valid[key] = validator.to_python(form_values.get(key))
except Invalid, e:
errors[key] = e
return valid, errors

-Chris
--
Christopher Lambacher
***@kateandchris.net
Chris Lambacher
2011-12-27 00:37:08 UTC
Permalink
Post by Chris Lambacher
Use the raw post values to render the page. You shouldn't need
validated values to do so.[...]
Of course Chris wanted to be brief in his answer; just one thing we
*Don't* use unvalidated/unfiltered values for re-rendering your form!
"><script language="">...
It will open your webapp to XSS, (XSRF, too?) or other kinds of attacks.
Mark is of course correct. I sometimes forget that not all template
languages automatically escape values inserted into templates. If you
are going to be re-inserting unvalidated post values into your
templates make sure that your template language (or form rendering
tool) properly escapes values inserted into the page.

-Chris
--
Christopher Lambacher
***@kateandchris.net
Daniel Lepage
2011-12-26 23:42:57 UTC
Permalink
Hi Mark,
Thanks for the tip! I'm already using AJAX, with JQuery instead of
Prototype (the rest of the project was already using JQuery), and I
agree that it makes the user experience much more streamlined.

Thanks,
Dan
Hi Dan!
No solution, just a remark: If you avoided the page-break by
submitting data asynchronously ("AJAX" (xml)) you can better please
your users. It worked very well for me. The corresponding JS code (you
will need Prototype for that) can be found here, along with a video of
[1] http://tgwidgets.ossdl.de/wiki/Scriptaculous/AJAJForms
--
Mark
Loading...