I found the fields keyword, and used that to limit the new post form.

For those searching the google groups archives for the answer to this:
to make some fields not show with SQLFORM, use fields = [<fields you
want to show>]. Only those fields will be included and validated from
the form. However, this creates incomplete table entries. I don't know
what the id is(this too is never mentioned what the variable name is
we're looking for from a successful insert), so I used the data I know
about(in this case subject+body) to do an update on that record.

-Tyler

On Jul 18, 3:40 am, mdipierro <[email protected]> wrote:
> On Jul 17, 9:41 pm, Tyler Laing <[email protected]> wrote:
>
> > I've been struggling with forms all day.
>
> > Here are the issues I've run into:
> > 1) There is no way to manually insert the hidden form values with
> > FORM. This should be an option, so people can customize the layout of
> > their forms better.
>
> form=FORM(....,hidden=dict(key=value,otherkey=othervalue)
>
> same with SQLFORM
>
> > 2) We need an ability to say what fields to NOT use in the table we
> > reference in SQLFORM. This, again, would make it easier to make a form
> > for JUST what we need. It means then that if we customize the forms as
> > per custom forms page here:http://web2py.com/book/default/section/7/7
> > that these forms will not ever validate.
>
> Not sure I understand. Are you talking about adding fields to a
> SQLFORM.
> You can in two ways
>
> form=SQLFORM(db.table)
> form.element('table').insert(TR('label',INPUT(_name='name'),'comment'))
>
> or
>
> form=SQLFORM.factory(db.table,Field('name'))  # new fields appended at
> the end
>
>
>
> > I'm writing a basic forum, and this is some of the code I've done.
>
> > db.define_table("sections", Field("name", 'string'), Field("desc",
> > 'string'), Field("ordering", 'integer'))
> > db.define_table("forums", Field("name", 'string'), Field("desc",
> > 'string'), Field("ordering", 'integer'),  Field("section_id",
> > 'reference sections'))
> > db.define_table("threads", Field("name", 'string',
> > requires=IS_NOT_EMPTY(), required=True), Field("author",
> > db.auth_user, default=user_id), Field("datetime", 'datetime'),
> > Field("forum_id", 'reference forums'))
> > db.define_table("posts", Field("subject", 'string'), Field("body",
> > 'string', length = 10000, requires=IS_NOT_EMPTY()), Field("author",
> > db.auth_user, default=user_id), Field("datetime", 'datetime'),
> > Field('postcount', 'integer'), Field('thread_id', 'reference
> > threads'))
>
> > Notice the various fields in posts?
>
> > The generated HTML for an SQLFORM of db.posts:
> > <form action="" enctype="multipart/form-data" method="post"><table><tr
> > id="posts_subject__row"><td class="w2p_fl"><label for="posts_subject"
> > id="posts_subject__label">Subject: </label></td><td
> > class="w2p_fw"><input class="string" id="posts_subject" name="subject"
> > type="text" value="" /></td><td class="w2p_fc"></td></tr><tr
> > id="posts_body__row"><td class="w2p_fl"><label for="posts_body"
> > id="posts_body__label">Body: </label></td><td class="w2p_fw"><textarea
> > class="string" cols="100" id="posts_body" name="body" rows="20"
> > type="text" value=""></textarea></td><td class="w2p_fc"></td></tr><tr
> > id="posts_author__row"><td class="w2p_fl"><label for="posts_author"
> > id="posts_author__label">Author: </label></td><td
> > class="w2p_fw"><select class="reference auth_user" id="posts_author"
> > name="author"><option value=""></option><option selected="selected"
> > value="1">Tyler Laing (1)</option><option value="2">Tyler Laing (2)</
> > option></select></td><td class="w2p_fc"></td></tr><tr
> > id="posts_datetime__row"><td class="w2p_fl"><label
> > for="posts_datetime" id="posts_datetime__label">Datetime: </label></
> > td><td class="w2p_fw"><input class="datetime" id="posts_datetime"
> > name="datetime" type="text" value="" /></td><td class="w2p_fc"></td></
> > tr><tr id="posts_postcount__row"><td class="w2p_fl"><label
> > for="posts_postcount" id="posts_postcount__label">Postcount: </label></
> > td><td class="w2p_fw"><input class="integer" id="posts_postcount"
> > name="postcount" type="text" value="" /></td><td class="w2p_fc"></td></
> > tr><tr id="posts_thread_id__row"><td class="w2p_fl"><label
> > for="posts_thread_id" id="posts_thread_id__label">Thread Id: </label></
> > td><td class="w2p_fw"><input class="reference threads"
> > id="posts_thread_id" name="thread_id" type="text" value="" /></td><td
> > class="w2p_fc"></td></tr><tr id="submit_record__row"><td
> > class="w2p_fl"></td><td class="w2p_fw"><input type="submit"
> > value="Submit" /></td><td class="w2p_fc"></td></tr></table><div
> > class="hidden"><input name="_formkey" type="hidden" value="bafdb421-
> > c041-4172-9e54-f98cfbe8adb4" /><input name="_formname" type="hidden"
> > value="posts_create" /></div></form>
>
> > This is for a forum! I don't want to show thread_id, postcount,
> > datetime, or the author field. I've tried what people have suggested
> > of doing:
>
> > db.posts.author.writeable = False
> > db.posts.datetime.writeable = False
> > db.posts.postcount.writeable = False
> > db.posts.thread_id.writeable = False
>
> should be "writable" not "writeable"
> db.posts.author.writable = db.posts.author.readable = False
> db.posts.datetime.writable =  db.posts.datetime.readable = False
> db.posts.postcount.writable =  db.posts.postcount.readable = False
> db.posts.thread_id.writable =  db.posts.thread_id.readable = False
>
> BTW, you should NOT have a field called 'datetime' because it is a
> reserved keyword and you can run into problems.
>
> > The fields still show up. So as per the custom forms, I'm trying this:
>
> > <table>
> > {{=form.custom.begin}}
> > {{form.custom.widget.body.tag='textarea'}}
> > {{form.custom.widget.body._postprocessing = TEXTAREA._postprocessing}}
> > {{form.custom.widget.body.attributes["_rows"] = 20}}
> > {{form.custom.widget.body.attributes["_cols"] = 100}}
> > {{=TR(TD("Subject:"), TD(form.custom.widget.subject))}}
> > {{=TR(TD(form.custom.widget.body, _colspan=2))}}
> > {{=TR(TD(form.custom.submit))}}
> > {{=form.custom.end}}
> > </table>
>
> The {{form.custom.widget.body...}} should be
> <tr><td>{{=form.custom.widget.body...}}....</td></tr>
>
> > While this changes the text input to a textarea, as I wanted, because
> > I lack the four fields above, it does not ever validate.  I've looked,
> > and looked, and looked, through many posts, and many threads.
>
> > The forms part of web2py needs to be A) better documented B) actually
> > be useable for a real world use case(forums).
>
> True. Anyway, hope this was helpful.
>
> Massimo

Reply via email to