From b932a1f01eaa2d98a1c7c6e447b1084b7fa0daa4 Mon Sep 17 00:00:00 2001
From: Drave Robber <DraveRobber@984338.no-reply.drupal.org>
Date: Sun, 22 Jul 2012 17:31:10 +0300
Subject: [PATCH] Configuration form needs some validation.

---
 botrules.module |   18 +++++++++++++++++-
 1 files changed, 17 insertions(+), 1 deletions(-)

diff --git a/botrules.module b/botrules.module
index cfad986..becaf8b 100644
--- a/botrules.module
+++ b/botrules.module
@@ -66,10 +66,11 @@ function botrules_configure() {
     '#type'          => 'textfield',
     '#title'         => t('Split long messages into chunks of'),
     '#prefix'        => '<div class="container-inline">',
-    '#suffix'        => t('characters') . '</div>',
+    '#suffix'        => t('characters (16 to 255)') . '</div>',
     '#size'          => 5,
     '#default_value' => variable_get('botrules_message_chunk', 255),
     '#required'      => TRUE,
+    '#element_validate' => array('botrules_validate_chunk'),
   );
   $form['messages']['botrules_queue_portion'] = array(
     '#type'          => 'textfield',
@@ -77,6 +78,7 @@ function botrules_configure() {
     '#size'          => 10,
     '#default_value' => variable_get('botrules_queue_portion', 3),
     '#required'      => TRUE,
+    '#element_validate' => array('element_validate_integer_positive'),
     '#description'   => t('Note: when a rule is fired by a bot event, any responses are not queued but sent instantly.'),
   );
   $form['messages']['botrules_log_invalid'] = array(
@@ -138,6 +140,20 @@ function botrules_configure() {
 }
 
 /**
+ * Helper function - validates chunk size for long messages.
+ *
+ * Technically, it is possible to break a message into one-character long
+ * chunks, which would however make little sense. 16 is just a voluntarily
+ * chosen 'sane' lower limit.
+ */
+function botrules_validate_chunk($element, &$form_state) {
+  $value = $element['#value'];
+  if (!is_numeric($value) || intval($value) != $value || $value < 16 || $value > 255) {
+    form_error($element, t('Chunk size must be a positive integer between 16 and 255.'));
+  }
+}
+
+/**
  * Helper function - prepare date format choices for queued message timestamp.
  */
 function botrules_date_format_choices() {
-- 
1.7.4.1

