web2py:crud 表单自定义小部件条件显示/隐藏
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在创建表单之前设置
readable
和writable
属性 - 在这种情况下,field1
将自动从表单中排除,您就赢了不必费心创建自定义表单并显式包含/排除它。Set the
readable
andwritable
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.