处理表单上锁定/禁用字段的最佳方法?

发布于 2024-12-12 05:02:52 字数 350 浏览 0 评论 0原文

我需要用户只能编辑网络表单上的某些字段,而不能编辑其他字段。例如,这可能是由于用户权限造成的。

我不确定处理这个问题的最佳方法。

  1. 将表单属性设置为禁用。这样做的问题是禁用的字段不会被提交,所以我要么需要包含隐藏的表单字段,要么需要在我的模型中满足这一点(即仅更新不应更改的字段)

  2. Set属性为只读。可能比上面的更好,因为这些字段会自动发布

  3. 根本不使用表单字段 - 只需回显 html 中的 var。可能包含隐藏的表单字段或仅更新模型中不应更改的字段

无论从用户体验的角度还是从一般应用程序的角度来看,我不确定处理此问题的最佳方法。

I need users to be able to only edit certain fields on a webform, and not others. This may be due to user privileges, for example.

I'm not sure on the best way to handle this.

  1. Set the form attribute to disabled. The problem with this is that disabled fields don't get submitted, so I either need to include hidden form fields, or I need to cater for this in my model (i.e. only update fields that shouldn't get changed)

  2. Set the attribute to readonly. Possibly better than the above because these fields automatically get posted

  3. Don't use a form field at all - just echo out the var in html. Possibly include a hidden form field or only update fields that shouldnt get changed in the model

I'm not sure on the best way to handle this, both from a UX point of view, and from a general application point of view.

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

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

发布评论

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

评论(4

终止放荡 2024-12-19 05:02:52

您应该始终在服务器端的模型中处理此问题。不知道你的应用会不会被公开使用?但是,在客户端更改必需或禁用的属性以便能够使用表单元素是相当容易的。

You should always handle this in your models, at the server side. I do not know if your application will be used publicly? But it's fairly easy to change the required or disabled attributes on the client side to be able to use the form elements.

请爱~陌生人 2024-12-19 05:02:52

我的选择是#3,或者根本不显示表单字段:如果用户无法对它们执行任何操作,则不应显示它们。如果有办法在当前交互中启用它,我会使用禁用字段 - 无法以任何明显方式启用的禁用字段会令人困惑,并且会导致我认为我做错了什么(如果可以的话)不要编辑它。

My choice would be #3 or not displaying the form fields at all: if the user can't do anything with them they shouldn't be shown. I'd use a disabled field if there's a way to enable it in the current interaction – a disabled field that can't be enabled in any obvious way is confusing and would lead me to think I'm doing something wrong if I can't edit it.

淡写薰衣草的香 2024-12-19 05:02:52

我一直使用禁用/隐藏组合解决方案,即

<input type="text" name="name-disabled" disabled="disabled" value="<?php echo $name; ?>" />
<input type="text" name="name" value="<?php echo $name; ?>" />

从未遇到过任何用户体验问题这样做。但无论如何,如果有人有更好的解决方案,请大声说出来。

I've always used the disabled/hidden combo solution, i.e.

<input type="text" name="name-disabled" disabled="disabled" value="<?php echo $name; ?>" />
<input type="text" name="name" value="<?php echo $name; ?>" />

Never had any UX issues doing it this way. But by all means if someone has a better solution, speak up.

无声情话 2024-12-19 05:02:52

UI 角度来看,不要向用户显示他们不需要看到的任何内容。并不是说没有地方存放只读信息,而是应该考虑每一位,看看它是否需要在特定的上下文中存在。更简单的表单可以减少混乱、减少用户时间并提高用户满意度。

我想这是一个观点问题,但我相信用户体验常常被忽视,而它通常应该是表单最重要的设计标准之一。

代码角度来看,你是对的,你必须稍微适应一下。隐藏表单字段当然是一种可能性,但它们很容易受到操纵——在您的特定情况下,这可能是也可能不是安全问题。

据推测,通过隐藏字段提交的任何内容都已经是已知值。您没有指定正在使用哪个框架(如果有),但可能可以在提交的表单数据进入数据库之前添加(并修改和验证)。如果用户无权更改某个字段,您可以从表单中省略该字段,并在将其写入数据库之前添加该字段及其默认值。

From the UI perspective, don't show the user anything they don't need to see. Not that there's no place for read-only information, but each bit should be considered to see if it needs to be there in a particular context. Simpler forms lead to less confusion, less user time, and happier users.

This is a matter of opinion, I guess, but I believe that user experience is often given short shrift when it should usually be among the most important design criteria for a form.

From the code perspective, you're right, you have to adapt a little. Hidden form fields are certainly a possibility, but they are vulnerable to manipulation—which may or may not be a security concern in your particular case.

Presumably anything that would be submitted via a hidden field is already a known value. You didn't specify which framework, if any, you're working with, but it's probably possible add to (and modify and validate) the submitted form data before it goes to the database. If a user doesn't have permission to alter a field, you can omit it from the form and add the field with its default value before you write it to the database.

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