From efc082c8672aaf654fffdcf3474525eda2a6ae89 Mon Sep 17 00:00:00 2001
From: Dan Chadwick <dan899@gmail.com>
Date: Wed, 24 Aug 2016 10:33:52 -0400
Subject: [PATCH] Issue #2661718 by Nicolaj, DanChadwick: Fixed no server side
 validation of required grid components.

---
 components/grid.inc | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/components/grid.inc b/components/grid.inc
index 9d978a0..8984606 100644
--- a/components/grid.inc
+++ b/components/grid.inc
@@ -225,6 +225,7 @@ function _webform_render_grid($component, $value = NULL, $filter = TRUE, $submis
     '#theme' => 'webform_grid',
     '#theme_wrappers' => array('webform_element'),
     '#process' => array('webform_expand_grid'),
+    '#element_validate' => array('webform_validate_grid'),
     '#translatable' => array('title', 'description', 'grid_options', 'grid_questions'),
   );
 
@@ -780,3 +781,23 @@ function _webform_grid_right_titles($element) {
 function _webform_grid_question_header($text) {
   return implode('/', array_filter(explode('|', $text)));
 }
+
+/**
+ * Element validation for Webform grid fields.
+ *
+ * Requires a component implementation because the default required validation
+ * passes when at least one value is supplied, rather than every value. This
+ * makes the server validation match the browser validation.
+ */
+function webform_validate_grid($element, $form_state) {
+  if ($element['#required']) {
+    $values = $form_state['input'];
+    foreach ($element['#parents'] as $key) {
+      $values = isset($values[$key]) ? $values[$key] : $values;
+    }
+    $nr_values = array_reduce($values, function($count, $item) { return $count + (int)!is_null($item); }, 0);
+    if (count($element['#grid_questions']) != $nr_values) {
+      form_error($element, t('!name field is required.', array('!name' => $element['#title'])));
+    }
+  }
+}
-- 
2.7.1.windows.2

