我已经为Gravity表单动态添加了复选框,但是当我选择最后2项并单击提交时,我收到错误消息“此字段是必需的”。如果我选择第一项或第二项,那么第三项则没有错误,表单已成功提交。
这是我的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;
}
由于
答案 0 :(得分:1)
好的,我发现了问题。我们需要关注输入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;
}
答案 1 :(得分:0)
也可以使用'form_field_value_ $ parameter'过滤器进行设置。给出一个复选框字段,其中包含选项“红色”,“绿色”,“蓝色”,“黄色”,“橙色”以及允许归档动态填充参数:“颜色”,以下内容将选中蓝色和绿色复选框。
add_filter( 'gform_field_value_color', 'set_checkbox' );
function set_checkbox( $form ) {
return 'Blue,Green';
}
我相信这最近被添加到重力表格中。