重力形式预选选择复选框字段

Gravity forms pre-selected choices checkboxes field

我正在使用带有一个单选按钮和一个复选框字段的重力表单,它们都有相同的选择。 我需要的是当我的用户在单选按钮上选择一个选项时,该选项会自动预 select 在复选框字段上。 他不必再 select 了。

我看到我必须使用一些过滤器,例如下面的过滤器,但我无法成功自定义它以执行我需要的操作。

如果有人能帮助我,他会救我的命。

<?php

# Make sure to replace {id} with your form's id
add_filter( 'gform_pre_render_{id}', 'my_populate_checkbox' );

function my_populate_checkbox( $form ) {

/**
* Loop through form fields
*
* Note we are using the `$field` array as a direct reference using `&`. 
* This means that changing its value will within the loop will 
* change the corresponding `$form` array item
*/
foreach( $form['fields'] as &$field ) {

# Make sure to change `1` to the ID of the checkbox field that you want to pre-populate
if( 1 === $field->id ) {

  /**
   * Loop through the choices for this checkbox
   *
   * Note again that we are passing `$choice` by reference in order to change the 
   * corresponding array item within the `$field` array
   */
  foreach( $field->choices as &$choice ) {

    /**
     * If this choice has a value of 'red' or 'blue', then make sure the checkbox is pre- 
   checked
     * by setting the `isSelected` parameter to `true`
     */

    if( 'red' === $choice['value'] || 'blue' === $choice['value'] ) {
      $choice['isSelected'] = true;
    }
   } # end foreach: $field->choices
   } # end if: $field->id equals 1
   } # end foreach: $form['fields']

    # return the altered `$form` array to Gravity Forms
   return $form;

    } # end: my_populate_checkbox

感谢Gravity Wiz team for answering to my question. Here a video to explain how to do so: https://www.loom.com/share/f20184e287be49e89116d4c641c77c11

想法很简单: 1- 你必须安装 Gravity Perks: Populate Anything 2- 您必须映射 2 种形式:一种带有数据,另一种将接收数据(这个包含您的表格) 3-这两个表格必须有相同的字段 4- 在映射表上,您必须根据您的需要创建条目

这有点棘手,但对我来说很完美

很高兴对你有用,珍。

对于需要这样做的其他人,我们已经编写了有关该过程的完整演练以及一个易于遵循的用例。

How to Check Checkboxes (and Other Choice-based Fields) Conditionally

这是一个快速速成课程:

  1. 创建映射表

    创建一个表单,用于跟踪当给定选项(在另一个字段中)被 selected 时应该选中哪些复选框。

  2. 提交表单条目

    现在您将完成一些数据输入。 Select 触发器选项,然后 select 每个应该为该选项选中的复选框。

  3. 复制映射表

    接下来,您将根据您的映射表单创建一个新表单。这是您的用户实际与之交互的表单。映射表就是为你准备的

  4. 映射您的字段

    最后,使用 Populate Anything 根据选项 selected 动态检查(即填充值)复选框字段,并从映射表单中检查该选项的复选框。