【问题标题】:Validate at least one field to be filled in, Gravity forms验证至少一个要填写的字段,重力表格
【发布时间】:2016-05-03 09:57:06
【问题描述】:

我一直在尝试为 Gravity Forms 插件创建一个自定义验证钩子。

验证检查是否已从一组字段中填写了至少一个字段。

查看下面的代码,我就是无法让它工作。我认为这与输入的变量有关,即使填写了一个字段,错误仍然显示在每个字段上?

add_filter( 'gform_field_validation_2', function ( $result, $value, $form, $field ) {

    if ( $field->type == 'number') {

        $a = rgar( $value, $field->id . '10' );
        $b = rgar( $value, $field->id . '12' );
        $c = rgar( $value, $field->id . '13' );
        $d = rgar( $value, $field->id . '14' );
        $e = rgar( $value, $field->id . '15' );
        $f = rgar( $value, $field->id . '17' );
        $g = rgar( $value, $field->id . '18' );
        $h = rgar( $value, $field->id . '20' );
        $i = rgar( $value, $field->id . '21' );
        $j = rgar( $value, $field->id . '22' );
        $k = rgar( $value, $field->id . '23' );


        if ( !empty($a) || !empty($b) || !empty($c) || !empty($d) || !empty($e) || !empty($f) || !empty($g) || !empty($h) || !empty($i) || !empty($j) || !empty($k) ) {
          $result['is_valid'] = true;
          $result['message'] ='';
        } else {
          $result['is_valid'] = false;
          $result['message']  = 'Please select a quantity of materials to order';
        }

    }

    return $result;
}, 10, 4 );

【问题讨论】:

  • 您要验证什么类型的字段?
  • 它是一个数字字段

标签: php wordpress forms validation gravity-forms-plugin


【解决方案1】:

我认为您可能应该使用“单选按钮”类型的字段。

无论如何,如果您的表单有多个“数字”字段并且您需要验证其中至少一个已被填写,那么您应该使用gform_validation 过滤器,因为您正在验证整个表单,而不仅仅是一个单个字段。

提示:为组中的每个字段添加自定义 css 类以识别它们。例如“验证数量”。

add_filter('gform_validation_2', 'quantity_validation', 1, 4);
function quantity_validation($validation_result) {
    if ($validation_result['is_valid']) {
        $valid=false;
        $form = $validation_result['form'];
        foreach( $form['fields'] as &$field ) {
            if ( strpos( $field->cssClass, 'validate-quantity' ) === false ) {
                continue;
            }
            $field_value = rgpost( "input_{$field['id']}" );
            if (!empty($field_value)) {
               $valid=true;
               break;
            }
        }

        if (!$valid) {
            $field["failed_validation"] = true;
            $field["validation_message"] = 'Please select a quantity of materials to order';
            $validation_result['form'] = $form;
        }

    }
    return $validation_result;
}

【讨论】:

    【解决方案2】:

    看来您应该将 if 子句更改为:

    if ( empty($a) || empty($b) || empty($c) || empty($d) || empty($e) || empty($f) || empty($g) || empty($h) || empty($i) || empty($j) || empty($k) ) {
    

    验证是否至少选择了一个选项。

    若要跳过非目标字段,请在上面的复杂代码之前添加以下代码:

    $target_fields = array('name_1', 'name_2');
    if (!in_array($field, $target_fields)) {
        $result['is_valid'] = true;
        $result['message']  = '';
    }
    

    【讨论】:

    • 这不会证明是错误的,因为它会确保它们都具有价值吗?同样在尝试时,我仍然收到影响所有字段的错误的初始问题,即使是那些未针对的字段
    【解决方案3】:

    所以这是一个工作版本(感谢 gform_validation 提示 Francisco R) - 走了一条略有不同的路线,但工作完美,以防万一有人对未来感兴趣!

    add_filter( 'gform_validation_2', 'custom_validation_2' );
    function custom_validation_2( $validation_result ) {
    
        // array of field IDs to be checked
        $field_ids = array (10, 12, 13, 14, 15, 17, 18, 20, 21, 23, 22);
    
        // get the form object from the validation result
        $form = $validation_result['form'];
    
        // counter to store how many fields have a value > 0 submitted
        $number_of_fields = 0;
    
        // loop through all the fields to be sure one has a value > 0
        foreach ( $field_ids as $input ) {
            // the rgpost string we are going to check
            $input_id = 'input_' . intval( $input );
    
            // the value that was submitted
            $input_value = rgpost ( $input_id );
    
            if ( $input_value > 0 ) {
                // if any field in the array has a value, we can just continue
                $number_of_fields++;
            } // end if
            else {
                // no value for this input, so continue without incrementing the counter
                continue;
            } // end else
    
        } // end foreach
    
        // check the $number_of_fields and if it is 0 return a validation error
        if ( $number_of_fields == 0 ){
    
            // set the form validation to false
            $validation_result['is_valid'] = false;
    
            // mark all the fields with a validation error
            foreach( $form['fields'] as &$field ) {
    
                // add a validation error to *all* the inputs if none were submitted > 0
                if ( in_array( $field->id, $field_ids ) ) {
                    $field->failed_validation = true;
                    $field->validation_message = 'Please select a quantity of materials to order from one or all of these fields.';
                } // end if
            } // end foreach
    
        } // end if
    
        // assign modified $form object back to the validation result
        $validation_result['form'] = $form;
        return $validation_result;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-05
      • 1970-01-01
      • 2011-07-21
      • 1970-01-01
      • 2014-06-17
      • 1970-01-01
      • 1970-01-01
      • 2014-10-16
      相关资源
      最近更新 更多