spivurno
9/12/2012 - 6:02 PM

Gravity Wiz // Require All Columns of List Field

Gravity Wiz // Require All Columns of List Field

<?php 

/**
* Require All Columns of List Field
* http://gravitywiz.com/2012/07/23/require-all-columns-of-list-field/
*/

add_filter('gform_validation', 'gwiz_require_all_list_columns');
function gwiz_require_all_list_columns($validation_result) {
    
    $form = $validation_result['form'];
    $new_validation_error = false;
    
    foreach($form['fields'] as &$field) {
        
        if($field['type'] != 'list' || !$field['isRequired'])
            continue;
        
        $values = rgpost("input_{$field['id']}");
        
        foreach($values as $value) {
            if(empty($value)) {
                $new_validation_error = true;
                $field['failed_validation'] = true;
                $field['validation_message'] = $field['errorMessage'] ? $field['errorMessage'] : 'All inputs must be filled out.';
            }
        }
        
    }
    
    $validation_result['form'] = $form;
    $validation_result['is_valid'] = $new_validation_error ? false : $validation_result['is_valid'];
    
    return $validation_result;
}