Problem with form and disappearing field data

236 views
Skip to first unread message

Jim Steil

unread,
Sep 7, 2022, 5:41:42 PM9/7/22
to py4web
I have a form with a textarea control.  If I paste in the following data and click submit, the data disappears and I get the 'Enter a value' message below the field (requires=IS_NOT_EMPTY() is set on the field.

The text is:

decided we’d have a blah blah blah they don’t need

It is text that I cut from a Word document and pasted in to my form field.  Notice the apostrophe's are not the same apostrophe character generated when I type on here

ex - '  is not equal to ’

In form.py, line 784 is the code:

original_value = post_vars.get(field.name)

Checking the debugger (pycharm) I see that before this line post_vars has a key of 'activity' (my field name) and the value I can see is the text string above.  

But, after line 784 executes, original_value is equal to None.

My question - is there a way to reliably escape those funky apostrophes (and other special characters) so that the field value is not lost?

Obviously I'm not very strong on encoding/decoding/internationalization and could use some pointers.

-Jim

CarlosDB

unread,
Sep 7, 2022, 6:48:34 PM9/7/22
to py4web
I think this problem is related to these 2 threads:


and with this commit:


Commenting out the added lines in the commit form works fine, but Grid search forms don't work with non-us-ascii characters


Let's see if we all manage to solve it...


Carlos.

Jim Steil

unread,
Sep 8, 2022, 7:41:37 AM9/8/22
to py4web
Thanks Carlos

I'll look at those today and see if I can make some sense of it.

-Jim

Jacinto Parga

unread,
Sep 18, 2022, 1:48:34 PM9/18/22
to py4web
I have seen the same issue. 

This happens when form or grid are used with non ascii characters: 

  1. Text area data are not saved in db, they are even deleted if there are some non asccii characters (example á, ñ, ü...)
  2. If a field string with a IS_NOT_EMPTY() validator is used then it rise an error 'Enter a value'

But

I can add non ascii characters when the dashboard db manager is used. Dashboard database manager works fine.


ombott   0.0.14
py4web  1.20220807.1
pydal       20220807.1

Jacinto Parga

unread,
Sep 19, 2022, 2:59:48 PM9/19/22
to py4web
This worked for me too, but I don't if there are side effects


Change line 143 in ombott/request/helpers.py
recode_unicode = False

Massimo

unread,
Sep 20, 2022, 1:23:33 AM9/20/22
to py4web
The proposed suggestion seems correct. I think it is easier to monkeypatch ombott in py4web that make a change to it.
I made the change in master and it seems to work for me. Please double check it. If no side effects we can release this.

Massimo

Val K

unread,
Sep 20, 2022, 2:33:37 AM9/20/22
to py4web
Already fixed in ombott 0.1.0
вторник, 20 сентября 2022 г. в 08:23:33 UTC+3, Massimo:

Massimo DiPierro

unread,
Sep 20, 2022, 2:44:20 AM9/20/22
to Val K, py4web
oops. missed that thank you Val.

--
You received this message because you are subscribed to the Google Groups "py4web" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4web+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/py4web/552bd3e1-7aa5-40bf-b737-f2db371cdf8an%40googlegroups.com.

Massimo

unread,
Sep 20, 2022, 11:02:20 AM9/20/22
to py4web
Unfortunately the patch still appears necessary with ombott 0.1.0.

Jim Steil

unread,
Sep 20, 2022, 12:22:25 PM9/20/22
to py4web
I have confirmed that this fixed my issue.

-Jim

Jacinto Parga

unread,
Sep 20, 2022, 12:28:28 PM9/20/22
to py4web
Mine too. It works, but you're right both changes are necessary: ombott 0.1.0 and py4web patch

Val K

unread,
Sep 20, 2022, 12:52:10 PM9/20/22
to py4web
which patch?

вторник, 20 сентября 2022 г. в 19:28:28 UTC+3, jpa...@gmail.com:

Jim Steil

unread,
Sep 20, 2022, 1:07:15 PM9/20/22
to py4web
Mine issue was fixed with the py4web patch.  I tested without the ombott patch and my issue was resolved.  I added the ombott patch and that (with the py4web patch) works too.

-Jim

Val K

unread,
Sep 20, 2022, 1:07:22 PM9/20/22
to py4web
if you mean recode_unicode = False in helpers.py  - I'm sure, this patch breaks   'application/x-www-form-urlencoded' forms 

вторник, 20 сентября 2022 г. в 19:52:10 UTC+3, Val K:

Christian Varas

unread,
Sep 20, 2022, 1:11:57 PM9/20/22
to Val K, py4web
I have  recode_unicode = False in helpers.py  for a month or more in a production app and I haven't experienced any undesired  effect after setting it to False.

Cheers.
Chris.

Jacinto Parga

unread,
Sep 20, 2022, 1:59:25 PM9/20/22
to py4web
No, I'm not talking about recode_unicode=False. 
I know that was not the solution but a tricky workaround.
 I am talking about py4web monkey patch, with the new version of ombott you have deployed .

Massimo

unread,
Sep 21, 2022, 12:02:50 AM9/21/22
to py4web
The monkey patch only sets recode_unicode=False. It works for me. For now I have released 20220920.1 including it.

CarlosDB

unread,
Sep 21, 2022, 8:32:15 PM9/21/22
to py4web

I’m very sorry :-( but I think that with this last modification we have returned to what I think is the origin of the problem.

Adding recode_unicode=False brings me back to problems with search_queries in Grid. With non-us-ascii characters there are encoded problems.

Removing the following, Grid works fine but Form doesn’t work fine.
line 126 py4web/core.py
FormsDict.recode_unicode = False


Removing this too, Form works fine but Grid doesn’t.

lineas 122, 123, 124, 125 de py4web/core.py
# monkey patching bottle/ombott
request.query.__class__.get, request.query.__class__.getraw = (
    request.query.__class__.getunicode,
    request.query.__class__.get,
)


If Grid works Form does not work well or vice versa.

Here is an example that is not working well with the latest modifications.

from py4web import action, request, abort, redirect, URL, Field
from .common import db, session
from py4web.utils.grid import Grid

db.define_table(
    't_test_grid',
    Field('f_test1', 'string', length=30),
)
db.commit()

if not db(db.t_test_grid).select():
    db.t_test_grid.insert(f_test1='niña')
    db.t_test_grid.insert(f_test1='niño')
    db.t_test_grid.insert(f_test1='camión')
    db.t_test_grid.insert(f_test1='cigüeña')

db.commit()

@action('grid_test')
@action('grid_test/<path:path>')
@action.uses('grid_test.html', db)
def grid_test(path=None):

    t_test_grid = db.t_test_grid
    grid = Grid(
        path=path,
        query=t_test_grid,
        search_queries=[
            ('Test', lambda value: t_test_grid.f_test1.contains(value)),
        ],
        rows_per_page=10,
    )

    return dict(grid=grid)

I don’t know if it is well understood.

I think the problem with Form started with this thread : https://groups.google.com/g/py4web/c/fuI6I_7LbSk/m/AZYm7kmqAAAJ


Carlos.

Massimo DiPierro

unread,
Sep 21, 2022, 8:48:34 PM9/21/22
to CarlosDB, py4web
Form should be the gold standard. Grid should just use form without transformation. will get to the bottom of it tonight. would help a lot to have a collection of examples to try in one place and turn that into a test.

Massimo

unread,
Sep 22, 2022, 12:48:03 AM9/22/22
to py4web
I tested forms with the latest code released yesterday. I could not reproduce any problem. I did not try grid but I checked the code and it use Form without additional parsing therefore I cannot see any problem.

Please make sure you pip install -r requirements.txt and run the latest py4web. If you have a problem please, post an example to reproduce.

Massimo

Val K

unread,
Sep 22, 2022, 4:32:20 AM9/22/22
to py4web
Grid uses request.query  (request.GET is the same, just an alias)  which is  'application/x-www-form-urlencoded'.
With monkey patch, all forms in 'application/x-www-form-urlencoded' will not work properly with unicode. 

 

четверг, 22 сентября 2022 г. в 07:48:03 UTC+3, Massimo:

Val K

unread,
Sep 22, 2022, 5:21:16 AM9/22/22
to py4web
@Massimo
https://github.com/valq7711/ombott/tree/forms-factory
made it configurable, so you can request._forms_factory = MyFormsClass



четверг, 22 сентября 2022 г. в 11:32:20 UTC+3, Val K:

Massimo DiPierro

unread,
Sep 22, 2022, 5:30:42 AM9/22/22
to Val K, py4web
thank you. will review this asap. i would still like an example of something that does not currently work well in grid.

py4web forms are multipart/form-data https://github.com/web2py/py4web/blob/master/py4web/utils/form.py#L295 whether or not used in grid.

grid uses request.query but only for things like page index which should not contain Unicode. 

is the problem with search in grid which may use GET forms? i did not try GET forms.

Val K

unread,
Sep 22, 2022, 5:31:00 AM9/22/22
to py4web
typo
request.__class__._forms_factory = MyFormsClass

четверг, 22 сентября 2022 г. в 12:21:16 UTC+3, Val K:

Val K

unread,
Sep 22, 2022, 5:32:16 AM9/22/22
to py4web
as I understand there is search-field in grid which can be unicode

четверг, 22 сентября 2022 г. в 12:31:00 UTC+3, Val K:

Val K

unread,
Sep 22, 2022, 5:38:02 AM9/22/22
to py4web
> is the problem with search in grid which may use GET forms? i did not try GET forms.
yeah,  GET forms should be recoded

четверг, 22 сентября 2022 г. в 12:32:16 UTC+3, Val K:

Val K

unread,
Sep 22, 2022, 5:52:40 AM9/22/22
to py4web
also consider that ombott sets `recode_unicode` attr  on instance of `_forms_factory`
so you can use that to adjust your custom class logic
четверг, 22 сентября 2022 г. в 12:38:02 UTC+3, Val K:

CarlosDB

unread,
Sep 22, 2022, 5:58:19 AM9/22/22
to py4web

Here is a quick example to reproduce the problem.

Currently Form works fine and the search in Grid does not work well.


controllers.py

from py4web import action, request, abort, redirect, URL, Field
from py4web.utils.form import Form
from py4web.utils.grid import Grid
from yatl.helpers import A


from .common import db, session

db.define_table(
    't_test_grid',
    Field('f_test1', 'string', length=30),
)

db.define_table(
    't_test_form',
    Field('f_test1', 'text', default=''),

)

if not db(db.t_test_grid).select():
    db.t_test_grid.insert(f_test1='niña')
    db.t_test_grid.insert(f_test1='niño')
    db.t_test_grid.insert(f_test1='camión')
    db.t_test_grid.insert(f_test1='cigüeña')

db.commit()


@action('grid_test')
@action('grid_test/<path:path>')
@action.uses('test.html', db)

def grid_test(path=None):

    t_test_grid = db.t_test_grid
    grid = Grid(
        path=path,
        query=t_test_grid,
        search_queries=[
            ('Test', lambda value: t_test_grid.f_test1.contains(value)),
        ],
        rows_per_page=10,
    )

    return dict(stuff=grid, link=A('form', _href=URL('form_test')))


@action('form_test', method=['GET', 'POST'])
@action.uses('test.html', db)
def form_test():

    t_test_form = db.t_test_form

    if not db(t_test_form).select():
        t_test_form.insert(f_test1='áéíóú ñÑ Üü')

    record = db(
        t_test_form
    ).select().last()

    form = Form(t_test_form, record)
    if form.accepted:
        redirect(URL('form_test'))

    return dict(stuff=form, link=A('grid', _href=URL('grid_test')))

test.html

[[extend 'layout.html']]
[[=link]]
[[=stuff]]


Carlos

Massimo

unread,
Sep 24, 2022, 2:19:10 AM9/24/22
to py4web
I think the source of problems is

The urlunquote function is used to parse the query string parameters and assumes they are latin1 even if they are unicode.
This happens before the parsed value are stored in FormsDict. This affects GET forms only (and indirectly grid).

I think that line should be removed or, at least, there should be a mechanism to monkey patch it. Currently there is not because the ombott.request module is shadowed by the request object.

Massimo

Massimo

unread,
Sep 24, 2022, 2:53:33 AM9/24/22
to py4web
I found a way to monkey patch this.
It is fragile as ombott may change.

Why does ombott need to parse query string variables as latin1 and not unicode? Maybe https://github.com/valq7711/ombott/blob/main/ombott/request/helpers.py#L12 can be removed or made configurable.

Val K

unread,
Sep 24, 2022, 3:47:46 PM9/24/22
to py4web
latin1 is the legacy of bottle
i did some research and it seems that there is no common convention about encoding between frameworks, some use latin1 as more robust, some use utf-8 as more convenient. 
So lets migrate to utf8

суббота, 24 сентября 2022 г. в 09:53:33 UTC+3, Massimo:

Massimo DiPierro

unread,
Sep 24, 2022, 4:24:17 PM9/24/22
to Val K, py4web

Val K

unread,
Sep 24, 2022, 7:34:13 PM9/24/22
to py4web
test please
суббота, 24 сентября 2022 г. в 23:24:17 UTC+3, Massimo:

Jacinto Parga

unread,
Sep 25, 2022, 5:37:16 AM9/25/22
to py4web
Hello.
Yes it works in first test. Tested addition, edition and search in http://127.0.0.1:8000/showcase/example_html_grid

Captura desde 2022-09-25 11-29-17.png

Jacinto Parga

unread,
Sep 25, 2022, 7:05:21 AM9/25/22
to py4web
Second test. I have upgraded py4web with pip and forms fail in edition and adition. 
Search in grid works according to the last changes.

Tested in:
http://127.0.0.1:8000/showcase/create_form

ombott 0.1.0
py4web 1.20220925.1
pydal 20220814.1

Looking for the problem

Jacinto Parga

unread,
Sep 25, 2022, 7:15:31 AM9/25/22
to py4web
The failure comes from form.py last commit:

It avoids register creation or edition in forms.

Don't know if it is the only one effect.

Massimo DiPierro

unread,
Sep 25, 2022, 11:36:43 AM9/25/22
to Jacinto Parga, py4web
will undo it for now.

Val K

unread,
Sep 25, 2022, 3:24:22 PM9/25/22
to py4web
Just let me know  if it is ready for pushing to pypi 

воскресенье, 25 сентября 2022 г. в 18:36:43 UTC+3, Massimo:

Val K

unread,
Sep 25, 2022, 3:39:12 PM9/25/22
to py4web
I also want to point out that, formally, this breaking change, as I've removed FormsDict methods that are no longer needed. So is there anyone who uses these methods? e.g. request.forms.getunicode or request.forms.decode (the same applies to request.query, request.POST, request.files)

воскресенье, 25 сентября 2022 г. в 22:24:22 UTC+3, Val K:

Kevin Keller

unread,
Sep 25, 2022, 3:44:01 PM9/25/22
to Val K, py4web
I used request.POST just last week. 

What am I supposed to use?

Request.json?



Val K

unread,
Sep 25, 2022, 3:47:59 PM9/25/22
to py4web
It is only about methods: decode and getunicode. - do you have request.POST.getunicode or request.POST.decode somewhere in your code?

воскресенье, 25 сентября 2022 г. в 22:44:01 UTC+3, kell...@gmail.com:

Val K

unread,
Sep 25, 2022, 4:08:59 PM9/25/22
to py4web
Instead `getunicode` use just `get`
`decode` no longer needed as all that stuff is alredy decoded. 
There maybe only issue: `decode` returns copy - so I will add copy method to return instance of FormsDict with dot-access supported. 
So instead `decode` use `copy` if you need a copy


воскресенье, 25 сентября 2022 г. в 22:47:59 UTC+3, Val K:

Val K

unread,
Sep 25, 2022, 5:16:34 PM9/25/22
to py4web
@Massimo  updated (added FormsDict.copy())
воскресенье, 25 сентября 2022 г. в 23:08:59 UTC+3, Val K:

Massimo DiPierro

unread,
Sep 25, 2022, 5:32:24 PM9/25/22
to Val K, py4web
all looks good. i would prefer if __getattr__ where to have default None instead of "".

Massimo DiPierro

unread,
Sep 25, 2022, 5:33:54 PM9/25/22
to Val K, py4web
one more thing. i would rename the request folder because it is shadowed by the request object. make monkey patching for testing hard.

Val K

unread,
Sep 27, 2022, 7:56:00 PM9/27/22
to py4web
изображение_2022-09-28_024915186.png

понедельник, 26 сентября 2022 г. в 00:33:54 UTC+3, Massimo:

Jacinto Parga

unread,
Sep 28, 2022, 3:52:35 AM9/28/22
to py4web
.../python3.10/site-packages/py4web/core.py", line 55, in <module>

from ombott.request.helpers import FormsDict
ModuleNotFoundError: No module named 'ombott.request'

ombott-1.0.0
Py4web: 1.20220926.1

Jacinto Parga

unread,
Sep 28, 2022, 4:07:22 AM9/28/22
to py4web

# Third party modules
import ombott as bottle
from ombott.request_pkg.helpers import FormsDict

CarlosDB

unread,
Sep 30, 2022, 6:41:43 AM9/30/22
to py4web
Now both Form and Grid are working perfectly for me!!!

Thanks!
Reply all
Reply to author
Forward
0 new messages