How to hide some Fields in Form?

110 views
Skip to first unread message

deim31

unread,
Oct 16, 2020, 2:11:13 PM10/16/20
to py4web
I tried to figure out how to hide some field that I wanna prefill - like
references.
I tried to add writable=False

Field('user_id', 'reference auth_user', writable=False),

And it worked but until I recreated db. After that it stop to work (OR
it didn't worked before and I didn't notice?)

In web2py there was fields= in SQLForm which selected which fields to
display.

What approach is in py4web for this?

Thanks, Shawn

Jim Steil

unread,
Oct 16, 2020, 3:27:54 PM10/16/20
to py4web
In py4web the Form init takes a hidden parameter that will create the field but hide it in the form.

But, setting readable and writable in your controller function should work.  I just tried it and it worked for me.


-Jim

deim31

unread,
Oct 19, 2020, 10:26:39 AM10/19/20
to py4...@googlegroups.com

Hi Jim, thanks for answer but:

when I set writable and readable in models.py or controllers.py it is hidden indeed but even I set:

    user = auth.get_user()
    form = Form(db.main)
    db.main.creator_id.writable = False
    db.main.creator_id.readable = False
    form.vars["creator_id"] = user['id']
    if form.accepted:
        redirect(URL("index"))

it saves null to DB to creator_id field. Maybe bad approach but I don't know how to set it correctly at the moment.

AFAIK hidden parameter adds hidden fields to form but I wanna hide and set some fields from db.

Thanks, Shawn

Dne 16. 10. 20 v 21:27 Jim Steil napsal(a):
--
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/16584f57-5c11-44bc-b682-79f8d3ae70a3o%40googlegroups.com.

Jim Steil

unread,
Oct 19, 2020, 10:59:52 AM10/19/20
to py4web
I just tried passing hidden fields to my form but am having some issues.  I think hidden fields will be updated (not positive).

I'll try to get time today to check it out.

-Jim
To unsubscribe from this group and stop receiving emails from it, send an email to py4web+unsubscribe@googlegroups.com.

Massimo DiPierro

unread,
Oct 19, 2020, 11:05:47 AM10/19/20
to Jim Steil, py4web
If one needs to pass data through a form and if that data is not to go into the database, then the proper way is using hidden fields but make sure the names do not conflict with with a “writable=True” field in the table.

For security I recommend passing a signed JWT token into a hidden field.

Massimo

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/d5bb4edc-aad0-4e11-9890-9d41fcf6990eo%40googlegroups.com.

deim31

unread,
Oct 19, 2020, 12:08:44 PM10/19/20
to py4...@googlegroups.com

Thanks Massimo,

but my situation:
I need to save field to db (the field is defined in models) but hide field from user and set it in controller.

I see no way at the moment.
Thanks, Shawn

Dne 19. 10. 20 v 17:05 Massimo DiPierro napsal(a):

Jacinto Parga

unread,
Oct 19, 2020, 3:27:51 PM10/19/20
to py4web
Simple solution (like SQLFORM.factory in web2py): 

user = auth.get_user()

form = Form(

Field('my_field_1),

Field('other_fields),

...

)
    if form.accepted:

        db.main.insert("creator_id"=user["id"], "my_field_1"=form.vars.my_field_1, "other_fields"=form.vars.other_fields)

        redirect(URL("index"))



Massimo

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/16584f57-5c11-44bc-b682-79f8d3ae70a3o%40googlegroups.com.

-- 
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/d5bb4edc-aad0-4e11-9890-9d41fcf6990eo%40googlegroups.com.
--
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.
Reply all
Reply to author
Forward
0 new messages