Commit e05b5857 authored by Dries's avatar Dries

- Patch #1174444 by Boobaa, xjm, Dave Reid: Make the _element_validate_()*...

- Patch #1174444 by Boobaa, xjm, Dave Reid: Make the _element_validate_()* functions in field.module available for all contrib modules to use.
parent 71d16d90
......@@ -4003,6 +4003,36 @@ function _form_set_class(&$element, $class = array()) {
}
}
/**
* Helper 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'])));
}
}
/**
* Helper 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'])));
}
}
/**
* Helper 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'])));
}
}
/**
* @} End of "defgroup form_api".
*/
......
......@@ -1174,33 +1174,3 @@ function theme_field($variables) {
return $output;
}
/**
* Helper 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'])));
}
}
/**
* Helper 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'])));
}
}
/**
* Helper 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'])));
}
}
......@@ -98,14 +98,14 @@ function number_field_instance_settings_form($field, $instance) {
'#title' => t('Minimum'),
'#default_value' => $settings['min'],
'#description' => t('The minimum value that should be allowed in this field. Leave blank for no minimum.'),
'#element_validate' => array('_element_validate_number'),
'#element_validate' => array('element_validate_number'),
);
$form['max'] = array(
'#type' => 'textfield',
'#title' => t('Maximum'),
'#default_value' => $settings['max'],
'#description' => t('The maximum value that should be allowed in this field. Leave blank for no maximum.'),
'#element_validate' => array('_element_validate_number'),
'#element_validate' => array('element_validate_number'),
);
$form['prefix'] = array(
'#type' => 'textfield',
......
......@@ -71,7 +71,7 @@ function text_field_settings_form($field, $instance, $has_data) {
'#default_value' => $settings['max_length'],
'#required' => TRUE,
'#description' => t('The maximum length of the field in characters.'),
'#element_validate' => array('_element_validate_integer_positive'),
'#element_validate' => array('element_validate_integer_positive'),
// @todo: If $has_data, add a validate handler that only allows
// max_length to increase.
'#disabled' => $has_data,
......@@ -227,7 +227,7 @@ function text_field_formatter_settings_form($field, $instance, $view_mode, $form
'#type' => 'textfield',
'#size' => 10,
'#default_value' => $settings['trim_length'],
'#element_validate' => array('_element_validate_integer_positive'),
'#element_validate' => array('element_validate_integer_positive'),
'#required' => TRUE,
);
}
......@@ -480,7 +480,7 @@ function text_field_widget_settings_form($field, $instance) {
'#title' => t('Size of textfield'),
'#default_value' => $settings['size'],
'#required' => TRUE,
'#element_validate' => array('_element_validate_integer_positive'),
'#element_validate' => array('element_validate_integer_positive'),
);
}
else {
......@@ -489,7 +489,7 @@ function text_field_widget_settings_form($field, $instance) {
'#title' => t('Rows'),
'#default_value' => $settings['rows'],
'#required' => TRUE,
'#element_validate' => array('_element_validate_integer_positive'),
'#element_validate' => array('element_validate_integer_positive'),
);
}
......
......@@ -42,7 +42,7 @@ function hook_field_settings_form($field, $instance, $has_data) {
'#title' => t('Maximum length'),
'#default_value' => $settings['max_length'],
'#required' => FALSE,
'#element_validate' => array('_element_validate_integer_positive'),
'#element_validate' => array('element_validate_integer_positive'),
'#description' => t('The maximum length of the field in characters. Leave blank for an unlimited size.'),
);
return $form;
......@@ -113,7 +113,7 @@ function hook_field_widget_settings_form($field, $instance) {
'#type' => 'textfield',
'#title' => t('Size of textfield'),
'#default_value' => $settings['size'],
'#element_validate' => array('_element_validate_integer_positive'),
'#element_validate' => array('element_validate_integer_positive'),
'#required' => TRUE,
);
}
......@@ -122,7 +122,7 @@ function hook_field_widget_settings_form($field, $instance) {
'#type' => 'textfield',
'#title' => t('Rows'),
'#default_value' => $settings['rows'],
'#element_validate' => array('_element_validate_integer_positive'),
'#element_validate' => array('element_validate_integer_positive'),
'#required' => TRUE,
);
}
......@@ -160,7 +160,7 @@ function hook_field_formatter_settings_form($field, $instance, $view_mode, $form
'#type' => 'textfield',
'#size' => 20,
'#default_value' => $settings['trim_length'],
'#element_validate' => array('_element_validate_integer_positive'),
'#element_validate' => array('element_validate_integer_positive'),
'#required' => TRUE,
);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment