重力形式中动态填充的复选框错误
Dynamically populated checkboxes error in Gravity Forms
我已将复选框动态添加到 Gravity 表单,但是当我 select 最后 2 个项目并单击提交时,我收到错误消息 "this field is required"。如果我 select 1st item or 2nd , 3rd 那么没有错误并且表单提交成功。
这是我的 php 代码。
add_filter( 'gform_pre_render_56', 'get_menu_options' );
add_filter( 'gform_pre_validation_56', 'get_menu_options' );
add_filter( 'gform_pre_submission_filter_56', 'get_menu_options' );
function get_menu_options($form){
/****get all beverages from post*****/
$beverages = array_filter( get_post_meta(get_the_ID(), 'wpcf-beverages', false) );
$form['fields'][22]->choices = set_field_choices($beverages);
return $form;
}
function set_field_choices($values){
$field_choices = array();
$isSelected = (count($values) == 1)?true:false;
foreach ($values as $value) {
$field_choices[] = array(
'text' => $value,
'value' => $value,
'isSelected' => $isSelected
);
}
return $field_choices;
}
这里是page link
谢谢
好的,我想出了问题。我们需要关注输入 id
add_filter( 'gform_pre_render_56', 'get_menu_options' );
add_filter( 'gform_pre_validation_56', 'get_menu_options' );
add_filter( 'gform_pre_submission_filter_56', 'get_menu_options' );
add_filter( 'gform_admin_pre_render_56', 'get_menu_options' );
function get_menu_options($form){
$buffetstations1 = array_filter( get_post_meta(get_the_ID(), 'wpcf-buffet-station-1', false) );
foreach ( $form[ 'fields' ] as $key => $field ) {
if( $field->id == 72 ){
if( count($buffetstations1) == 0 ){
unset ( $form['fields'][$key]);
}
$choices = array();
$inputs = array();
$field_id = $field->id;
$input_id = 1;
foreach ( $buffetstations1 as $buffetstation1 ) {
//skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
if ( $input_id % 10 == 0 ) {
$input_id++;
}
$choices[] = array( 'value' => $buffetstation1, 'text' => $buffetstation1 );
$inputs[] = array( 'label' => $buffetstation1, 'id' => "{$field_id}.{$input_id}" );
$input_id++;
}
$field->choices = $choices;
$field->inputs = $inputs;
}
}
return $form;
}
也可以用'form_field_value_$parameter'过滤器设置。给定一个带有选项 'Red'、'Green'、'Blue'、'Yellow' 'Orange' 的复选框字段和允许动态填充的字段参数:'color'以下将选中蓝色和绿色复选框。
add_filter( 'gform_field_value_color', 'set_checkbox' );
function set_checkbox( $form ) {
return 'Blue,Green';
}
我相信这是最近添加到重力形式中的。
我已将复选框动态添加到 Gravity 表单,但是当我 select 最后 2 个项目并单击提交时,我收到错误消息 "this field is required"。如果我 select 1st item or 2nd , 3rd 那么没有错误并且表单提交成功。
这是我的 php 代码。
add_filter( 'gform_pre_render_56', 'get_menu_options' );
add_filter( 'gform_pre_validation_56', 'get_menu_options' );
add_filter( 'gform_pre_submission_filter_56', 'get_menu_options' );
function get_menu_options($form){
/****get all beverages from post*****/
$beverages = array_filter( get_post_meta(get_the_ID(), 'wpcf-beverages', false) );
$form['fields'][22]->choices = set_field_choices($beverages);
return $form;
}
function set_field_choices($values){
$field_choices = array();
$isSelected = (count($values) == 1)?true:false;
foreach ($values as $value) {
$field_choices[] = array(
'text' => $value,
'value' => $value,
'isSelected' => $isSelected
);
}
return $field_choices;
}
这里是page link
谢谢
好的,我想出了问题。我们需要关注输入 id
add_filter( 'gform_pre_render_56', 'get_menu_options' );
add_filter( 'gform_pre_validation_56', 'get_menu_options' );
add_filter( 'gform_pre_submission_filter_56', 'get_menu_options' );
add_filter( 'gform_admin_pre_render_56', 'get_menu_options' );
function get_menu_options($form){
$buffetstations1 = array_filter( get_post_meta(get_the_ID(), 'wpcf-buffet-station-1', false) );
foreach ( $form[ 'fields' ] as $key => $field ) {
if( $field->id == 72 ){
if( count($buffetstations1) == 0 ){
unset ( $form['fields'][$key]);
}
$choices = array();
$inputs = array();
$field_id = $field->id;
$input_id = 1;
foreach ( $buffetstations1 as $buffetstation1 ) {
//skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
if ( $input_id % 10 == 0 ) {
$input_id++;
}
$choices[] = array( 'value' => $buffetstation1, 'text' => $buffetstation1 );
$inputs[] = array( 'label' => $buffetstation1, 'id' => "{$field_id}.{$input_id}" );
$input_id++;
}
$field->choices = $choices;
$field->inputs = $inputs;
}
}
return $form;
}
也可以用'form_field_value_$parameter'过滤器设置。给定一个带有选项 'Red'、'Green'、'Blue'、'Yellow' 'Orange' 的复选框字段和允许动态填充的字段参数:'color'以下将选中蓝色和绿色复选框。
add_filter( 'gform_field_value_color', 'set_checkbox' );
function set_checkbox( $form ) {
return 'Blue,Green';
}
我相信这是最近添加到重力形式中的。