Commit 73deaa34 authored by Dries's avatar Dries

- Patch #735894 by yched: critical bug: fields with unlimited maximum length cause DB errors.

parent e6059d4b
......@@ -120,6 +120,33 @@ function list_field_settings_form($field, $instance, $has_data) {
'#access' => empty($settings['allowed_values_function']),
);
if ($field['type'] == 'list_boolean') {
$values = list_extract_allowed_values($settings['allowed_values']);
$off_value = array_shift($values);
$on_value = array_shift($values);
$form['allowed_values'] = array(
'#type' => 'markup',
'#description' => '',
'#input' => TRUE,
'#value_callback' => 'list_boolean_allowed_values_callback',
'#access' => empty($settings['allowed_values_function']),
);
$form['allowed_values']['on'] = array(
'#type' => 'textfield',
'#title' => t('On value'),
'#default_value' => $on_value,
'#required' => FALSE,
'#description' => t('If left empty, "1" will be used.'),
);
$form['allowed_values']['off'] = array(
'#type' => 'textfield',
'#title' => t('Off value'),
'#default_value' => $off_value,
'#required' => FALSE,
'#description' => t('If left empty, "0" will be used.'),
);
}
// Alter the description for allowed values depending on the widget type.
if ($instance['widget']['type'] == 'options_onoff') {
$form['allowed_values']['#description'] .= '<p>' . t("For a 'single on/off checkbox' widget, define the 'off' value first, then the 'on' value in the <strong>Allowed values</strong> section. Note that the checkbox will be labeled with the label of the 'on' value.") . '</p>';
......@@ -164,15 +191,18 @@ function list_allowed_values_setting_validate($element, &$form_state) {
form_error($element, t('Allowed values list: keys must be integers.'));
break;
}
elseif ($field_type == 'list_boolean' && !in_array($key, array('0', '1'))) {
form_error($element, t('Allowed values list: keys must be either 0 or 1.'));
break;
}
}
}
// Check that boolean fields get two values.
if ($field_type == 'list_boolean' && count($values) != 2) {
form_error($element, t('Allowed values list: two values are required.'));
/**
* Form element #value_callback: assembles the allowed values for 'boolean' fields.
*/
function list_boolean_allowed_values_callback($element, $edit = FALSE) {
if ($edit !== FALSE) {
$on = $edit['on'];
$off = $edit['off'];
$edit = "0|$off\n1|$on";
return $edit;
}
}
......
......@@ -154,18 +154,6 @@ class ListFieldUITestCase extends FieldTestCase {
$edit = array($element_name => "1|one\n" . $this->randomName(256) . "|two");
$this->drupalPost($admin_path, $edit, t('Save settings'));
$this->assertText("each key must be a string at most 255 characters long", t('Form validation failed.'));
// Test 'List (boolean)' field type.
$admin_path = $this->createListFieldAndEdit('list_boolean');
// Check that invalid option keys are rejected.
$edit = array($element_name => "1|one\n2|two");
$this->drupalPost($admin_path, $edit, t('Save settings'));
$this->assertText("keys must be either 0 or 1", t('Form validation failed.'));
//Check that missing option causes failure.
$edit = array($element_name => "1|one");
$this->drupalPost($admin_path, $edit, t('Save settings'));
$this->assertText("two values are required", t('Form validation failed.'));
}
/**
......
......@@ -122,8 +122,8 @@ function text_field_settings_form($field, $instance, $has_data) {
'#type' => 'textfield',
'#title' => t('Maximum length'),
'#default_value' => $settings['max_length'],
'#required' => FALSE,
'#description' => t('The maximum length of the field in characters. Leave blank for an unlimited size.'),
'#required' => TRUE,
'#description' => t('The maximum length of the field in characters.'),
'#element_validate' => array('_element_validate_integer_positive'),
// @todo: If $has_data, add a validate handler that only allows
// max_length to increase.
......
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