summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYves Chedemois2008-12-05 23:27:57 (GMT)
committer Yves Chedemois2008-12-05 23:27:57 (GMT)
commit4a47540cb6b688e431ddc494c32efecd910abb83 (patch)
tree3a016401b03c3f63f6666e586f42faa68ff5e8a2
parent85cb17ef301b53d9d730233dfd2909852c4cf6b7 (diff)
#343306 Validate text 'max length' to be a positive integer
+ abstract a few element validators
-rw-r--r--CHANGELOG.txt1
-rw-r--r--includes/content.admin.inc32
-rw-r--r--modules/number/number.module4
-rw-r--r--modules/text/text.module19
4 files changed, 37 insertions, 19 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 1e67edb..e891dd0 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -7,6 +7,7 @@ CCK 6.x-2.x
- Views integration: use shorter titles in Views admin summaries (see http://drupal.org/node/326034).
- #334290 by drewish - Userreference: user names not displayed in Views summaries.
- #343138 by duellj - Fix tokens for empty noderef / userref fields.
+- #343306 Validate text 'max length' to be a positive integer
CCK 6.x-2.1
===========
diff --git a/includes/content.admin.inc b/includes/content.admin.inc
index 18940cd..9e69637 100644
--- a/includes/content.admin.inc
+++ b/includes/content.admin.inc
@@ -1828,4 +1828,34 @@ function content_field_replace($nid, $updates) {
}
node_save($node);
return $node;
-} \ No newline at end of file
+}
+
+/**
+ * Halper form element validator : integer.
+ */
+function _element_validate_integer($element, &$form_state) {
+ $value = $element['#value'];
+ if ($value !== '' && (!is_numeric($value) || intval($value) != $value)) {
+ form_error($element, t('%name must be an integer.', array('%name' => $element['#title'])));
+ }
+}
+
+/**
+ * Halper form element validator : integer > 0.
+ */
+function _element_validate_integer_positive($element, &$form_state) {
+ $value = $element['#value'];
+ if ($value !== '' && (!is_numeric($value) || intval($value) != $value || $value <= 0)) {
+ form_error($element, t('%name must be a positive integer.', array('%name' => $element['#title'])));
+ }
+}
+
+/**
+ * Halper form element validator : number.
+ */
+function _element_validate_number($element, &$form_state) {
+ $value = $element['#value'];
+ if ($value != '' && !is_numeric($value)) {
+ form_error($element, t('%name must be a number.', array('%name' => $element['#title'])));
+ }
+}
diff --git a/modules/number/number.module b/modules/number/number.module
index 334e98b..920380b 100644
--- a/modules/number/number.module
+++ b/modules/number/number.module
@@ -55,13 +55,13 @@ function number_field_settings($op, $field) {
$form['min'] = array(
'#type' => 'textfield',
'#title' => t('Minimum'),
- '#element_validate' => array('_number_widget_settings_min_validate'),
+ '#element_validate' => array('_element_validate_number'),
'#default_value' => is_numeric($field['min']) ? $field['min'] : '',
);
$form['max'] = array(
'#type' => 'textfield',
'#title' => t('Maximum'),
- '#element_validate' => array('_number_widget_settings_max_validate'),
+ '#element_validate' => array('_element_validate_number'),
'#default_value' => is_numeric($field['max']) ? $field['max'] : '',
);
if ($field['type'] == 'number_decimal') {
diff --git a/modules/text/text.module b/modules/text/text.module
index 5f8648d..95357c0 100644
--- a/modules/text/text.module
+++ b/modules/text/text.module
@@ -63,6 +63,7 @@ function text_field_settings($op, $field) {
'#title' => t('Maximum length'),
'#default_value' => is_numeric($field['max_length']) ? $field['max_length'] : '',
'#required' => FALSE,
+ '#element_validate' => array('_element_validate_integer_positive'),
'#description' => t('The maximum length of the field in characters. Leave blank for an unlimited size.'),
);
$form['allowed_values_fieldset'] = array(
@@ -314,7 +315,7 @@ function text_widget_settings($op, $widget) {
'#type' => 'textfield',
'#title' => t('Size of textfield'),
'#default_value' => $size,
- '#element_validate' => array('_text_widget_settings_size_validate'),
+ '#element_validate' => array('_element_validate_integer_positive'),
'#required' => TRUE,
);
}
@@ -323,7 +324,7 @@ function text_widget_settings($op, $widget) {
'#type' => 'textfield',
'#title' => t('Rows'),
'#default_value' => $rows,
- '#element_validate' => array('_text_widget_settings_row_validate'),
+ '#element_validate' => array('_element_validate_integer_positive'),
'#required' => TRUE,
);
$form['size'] = array('#type' => 'hidden', '#value' => $size);
@@ -335,20 +336,6 @@ function text_widget_settings($op, $widget) {
}
}
-function _text_widget_settings_row_validate($element, &$form_state) {
- $value = $form_state['values']['rows'];
- if (!is_numeric($value) || intval($value) != $value || $value <= 0) {
- form_error($element, t('"Rows" must be a positive integer.'));
- }
-}
-
-function _text_widget_settings_size_validate($element, &$form_state) {
- $value = $form_state['values']['size'];
- if (!is_numeric($value) || intval($value) != $value || $value <= 0) {
- form_error($element, t('"Size" must be a positive integer.'));
- }
-}
-
/**
* Implementation of hook_widget().
*