如何使用 QuickForm 添加禁用的选择选项?

发布于 2024-08-18 19:21:30 字数 620 浏览 10 评论 0原文

我有使用 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 技术交流群。

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

发布评论

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

评论(2

生生不灭 2024-08-25 19:21:30

好的,在深入研究 QuickForm 文档后,我明白了这一点。解决方案是不使用数组填充选择小部件,而是手动构建选择元素并将其添加到表单中。

最初,我有这样的:

function dbArray( $tableName, $fieldName ) {
    $query = <<< EOT
SELECT   `id`, `$fieldName`
FROM     `$tableName`
ORDER BY `$fieldName`
EOT;

    $link = connectToDatabase();
    $result = mysql_query( $query, $link );
    while ( $rec = mysql_fetch_assoc( $result ) );
    {
        $array[$rec['id']] = $rec[$fieldName];
    }

    return $array;
}

function statesArray() {
    return dbArray( 'states', 'name' );
}

$form = new HTML_QuickForm( 'account', 'POST' );
$form->addElement( 'select', 'state_id', 'State:', statesArray() );

我做了一个版本,其中 array( 'none' => 'Please select a State' ) 在返回数组之前被添加到 dbArray 调用之前到调用代码,但这并没有使该选项被禁用。解决方法是添加规则来确认选择是数字($form->addRule( 'state_id', 'You Must select a state.', 'numeric' ))。但我仍然不喜欢它是可选择的。这是我找到的解决方案。

function statesSelect() {
    $select = HTML_QuickForm::createElement( 'select' );
    $select->addOption( 'Select a State', '', array( 'disabled' => 'disabled' ) );
    $select->addOption( '-', '', array( 'disabled' => 'disabled' ) );

    $statesArray = dbArray( 'states', 'name' );
    foreach ( $statesArray as $id => $name ) {
        $select->addOption( $name, $id );
    }

    return $select;
}

$form = new HTML_QuickForm( 'account', 'POST' );
$form->addElement( statesSelect() );
$form->addRule( 'state_id', 'You must select a state.', 'required' );

我希望这对其他人有帮助。 :)

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:

function dbArray( $tableName, $fieldName ) {
    $query = <<< EOT
SELECT   `id`, `$fieldName`
FROM     `$tableName`
ORDER BY `$fieldName`
EOT;

    $link = connectToDatabase();
    $result = mysql_query( $query, $link );
    while ( $rec = mysql_fetch_assoc( $result ) );
    {
        $array[$rec['id']] = $rec[$fieldName];
    }

    return $array;
}

function statesArray() {
    return dbArray( 'states', 'name' );
}

$form = new HTML_QuickForm( 'account', 'POST' );
$form->addElement( 'select', 'state_id', 'State:', statesArray() );

I did a version where array( 'none' => 'Please select a State' ) was prepended to the dbArray 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.

function statesSelect() {
    $select = HTML_QuickForm::createElement( 'select' );
    $select->addOption( 'Select a State', '', array( 'disabled' => 'disabled' ) );
    $select->addOption( '-', '', array( 'disabled' => 'disabled' ) );

    $statesArray = dbArray( 'states', 'name' );
    foreach ( $statesArray as $id => $name ) {
        $select->addOption( $name, $id );
    }

    return $select;
}

$form = new HTML_QuickForm( 'account', 'POST' );
$form->addElement( statesSelect() );
$form->addRule( 'state_id', 'You must select a state.', 'required' );

I hope this helps someone else. :)

心作怪 2024-08-25 19:21:30

从技术上讲,最好的解决方案是使用 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 first option in the select 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.

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