web2py:crud 表单自定义小部件条件显示/隐藏

发布于 2025-01-07 07:05:53 字数 587 浏览 4 评论 0原文

我在 CRUD 表单中使用自定义小部件,如下所示:

{{=form.custom.begin}}
{{if condition:}}
    {{=form.custom.widget.field1}}
{{pass}}
{{=form.custom.submit}}
{{=form.custom.end}}

field1 与 auth_user 表有关系。在我的控制器中,我有:

form.custom.widget['field1'] = dropdown

if not condition:
    db.admission.field1.readable = db.admission.field1.writable = False
    db.admission.field1.default = auth.user.id

我希望小部件仅在条件为真时显示。如果条件不成立,我希望 field1 默认为当前登录用户。但是,当条件不成立时,我会收到错误:

field1 error: value not in database

我做错了什么?

I'm using custom widget in my crud form like this:

{{=form.custom.begin}}
{{if condition:}}
    {{=form.custom.widget.field1}}
{{pass}}
{{=form.custom.submit}}
{{=form.custom.end}}

field1 has a relationship to the auth_user table. In my controller I have:

form.custom.widget['field1'] = dropdown

if not condition:
    db.admission.field1.readable = db.admission.field1.writable = False
    db.admission.field1.default = auth.user.id

I want the widget to show only when the condition is true. If the condition is not true, I want field1 to default to the current logged-in user. But, when the condition is not true, I get the error:

field1 error: value not in database

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄人书 2025-01-14 07:05:53

在创建表单之前设置 readablewritable 属性 - 在这种情况下,field1 将自动从表单中排除,您就赢了不必费心创建自定义表单并显式包含/排除它。

Set the readable and writable attributes before creating the form -- in that case, field1 will simply be excluded from the form automatically, and you won't have to bother with creating a custom form and explicitly including/excluding it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文