如何使用 QuickForm 添加禁用的选择选项?
我有使用 QuickForm 的代码,该代码使用以下内容创建一个选择小部件:
$form->addElement( 'select', 'state_id', 'State:', statesArray() );
statesArray()
查询数据库以获取可用的状态,并返回一个关联数组,其中 id 链接到状态名称。我在整个解决方案中使用了类似的技术。
我想做的是在这个数组前面加上两个被禁用的选项,这样默认情况下选择菜单就会显示类似“请选择一个状态”的内容,后跟一个破折号,这两个选项都被禁用。如果我没有使用 QuickForm,则选择将具有以下作为前两个选项:
<option value="" disabled="disabled">Select a State</option>
<option value="" disabled="disabled">-</option>
两个选项均被禁用,如果用户将该选项保留在第一个值上,则选择小部件将提交一个空值,该值将被表单检查代码。
有没有办法用 QuickForm 来做到这一点?
谢谢, 查克
I have code using QuickForm that creates a select widget with the following:
$form->addElement( 'select', 'state_id', 'State:', statesArray() );
statesArray()
queries the database to get the states available and returns an associative array with the ids linked to the state names. I'm using a similar technique throughout the solution.
What I'd like to do is prepend this array with two options that are disabled, so that by default the select menu says something like "Please select a state" followed by a dash, both of which are disabled. If I weren't using QuickForm, the select would have the following as the first two options:
<option value="" disabled="disabled">Select a State</option>
<option value="" disabled="disabled">-</option>
Both options are disabled, and if the user leaves the option on the first value, the select widget submits an empty value which is made invalid by the form checking code.
Is there a way to do this with QuickForm?
Thanks,
Chuck
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,在深入研究 QuickForm 文档后,我明白了这一点。解决方案是不使用数组填充选择小部件,而是手动构建选择元素并将其添加到表单中。
最初,我有这样的:
我做了一个版本,其中
array( 'none' => 'Please select a State' )
在返回数组之前被添加到dbArray
调用之前到调用代码,但这并没有使该选项被禁用。解决方法是添加规则来确认选择是数字($form->addRule( 'state_id', 'You Must select a state.', 'numeric' )
)。但我仍然不喜欢它是可选择的。这是我找到的解决方案。我希望这对其他人有帮助。 :)
OK, after digging much deeper into the QuickForm documentation, I figured this out. The solution is to not populate the select widget with an array, but to build the select element manually add this to the form.
Originally, I had this:
I did a version where
array( 'none' => 'Please select a State' )
was prepended to thedbArray
call before returning the array to the calling code, but this didn't make the option disabled. Adding a rule to confirm that the choice is numeric was the workaround ($form->addRule( 'state_id', 'You must select a state.', 'numeric' )
). But I still didn't like that it was selectable. Here's the solution I found.I hope this helps someone else. :)
从技术上讲,最好的解决方案是使用
optgroup
,但浏览器通常不会默认使用此值,而是使用select
中的第一个option
代码>组。为什么不让您的
Select State
具有“None”值(类似于您给定的老式解决方案)并使其保持启用状态,然后让小部件在将其留空时将表单返回为无效?这超出了 QuickForm 的范围吗?我认为大多数用户不会注意到某些功能是启用还是禁用,禁用的唯一好处是使其不可选择。
您的目标是验证输入吗?如果是这样,为什么不在表单和 QuickForm 之间添加一些 JavaScript 来检查用户在提交之前是否选择了禁用(或 value="Null")元素?
显然,我需要阅读 QuickForm 文档才能了解完整情况,但根据我从您的示例中获得的信息,您只需添加
Please Choose State =>; None
到 state_array ,然后让您计划使用的任何形式验证器不接受“None”作为有效输入。Techinically, the best solution would be to use
optgroup
, but browsers usually won't default to this value, but instead to the firstoption
in theselect
group.Why not have your
Select State
have a value of "None" (similar to your given old-school solution) and have leave it enabled, then have the widget return the form as invalid if they leave it blank? Is this outside of the scope of QuickForm?I think most users don't notice if something is enabled or disabled, and the only benefit of disabling is to make it un-selectable.
Is your goal geared toward validating the input? If so, why not just add some javascript between the form and QuickForm to check if the user has selected a disabled (or value="Null") element before submitting it?
Obviously I need to read the QuickForm documentation to get the full picture, but based on what I can get from your example, you could just add
Please Choose State => None
to the state_array and then have whatever form validator you plan to use not accept "None" as a valid input.