function content_field_edit_form_validate in Content Construction Kit (CCK) 6.3
Same name and namespace in other branches
- 6.2 includes/content.admin.inc \content_field_edit_form_validate()
Validate a field's settings.
File
- includes/
content.admin.inc, line 1220 - Administrative interface for content type creation.
Code
function content_field_edit_form_validate($form, &$form_state) {
$form_values = $form_state['values'];
if (isset($form_state['change_basic']) || $form_values['op'] == t('Change basic information')) {
return;
}
module_load_include('inc', 'content', 'includes/content.crud');
$previous_field = unserialize($form_values['previous_field']);
$field = content_field_instance_expand($form_values);
$field['db_storage'] = content_storage_type($field);
$field_types = _content_field_types();
$field_type = $field_types[$field['type']];
$widget_types = _content_widget_types();
$widget_type = $widget_types[$field['widget']['type']];
if ($dropped_data = content_alter_db_analyze($previous_field, $field)) {
// @TODO
// This is a change that might result in loss of data.
// Add a confirmation form here.
// dsm($dropped_data);
}
module_invoke($widget_type['module'], 'widget_settings', 'validate', array_merge($field, $form_values));
module_invoke($field_type['module'], 'field_settings', 'validate', array_merge($field, $form_values));
// If content.module is handling the default value,
// validate the result using the field validation.
if (content_callback('widget', 'default value', $field) == CONTENT_CALLBACK_DEFAULT) {
// If this is a programmed form, get rid of the default value widget,
// we have the default values already.
if ($form['#programmed']) {
form_set_value(array(
'#parents' => array(
'default_value_widget',
),
), NULL, $form_state);
return;
}
if (isset($form_values['default_value_php']) && ($php = trim($form_values['default_value_php']))) {
$error = FALSE;
ob_start();
$return = eval($php);
ob_end_clean();
if (!is_array($return)) {
$error = TRUE;
}
else {
foreach ($return as $item) {
if (!is_array($item)) {
$error = TRUE;
break;
}
}
}
if ($error) {
$db_info = content_database_info($field);
$columns = array_keys($db_info['columns']);
foreach ($columns as $key => $column) {
$columns[$key] = t("'@column' => value for @column", array(
'@column' => $column,
));
}
$sample = t("return array(\n 0 => array(@columns),\n // You'll usually want to stop here. Provide more values\n // if you want your 'default value' to be multi-valued:\n 1 => array(@columns),\n 2 => ...\n);", array(
'@columns' => implode(', ', $columns),
));
form_set_error('default_value_php', t('The default value PHP code returned an incorrect value.<br/>Expected format: <pre>!sample</pre> Returned value: @value', array(
'!sample' => $sample,
'@value' => print_r($return, TRUE),
)));
return;
}
else {
$default_value = $return;
$is_code = TRUE;
form_set_value(array(
'#parents' => array(
'default_value_php',
),
), $php, $form_state);
form_set_value(array(
'#parents' => array(
'default_value',
),
), array(), $form_state);
}
}
elseif (!empty($form_values['default_value_widget'])) {
// Fields that handle their own multiple values may use an expected
// value as the top-level key, so just pop off the top element.
$key = array_shift(array_keys($form_values['default_value_widget']));
$default_value = $form_values['default_value_widget'][$key];
$is_code = FALSE;
form_set_value(array(
'#parents' => array(
'default_value_php',
),
), '', $form_state);
form_set_value(array(
'#parents' => array(
'default_value',
),
), $default_value, $form_state);
}
if (isset($default_value)) {
$node = array();
$node[$form_values['field_name']] = $default_value;
$field['required'] = FALSE;
$field_function = $field_type['module'] . '_field';
$errors_before = form_get_errors();
// Widget now does its own validation, should be no need
// to add anything for widget validation here.
if (function_exists($field_function)) {
$field_function('validate', $node, $field, $default_value, $form, NULL);
}
// The field validation routine won't set an error on the right field,
// so set it here.
$errors_after = form_get_errors();
if (count($errors_after) > count($errors_before)) {
if (trim($form_values['default_value_php'])) {
form_set_error('default_value_php', t("The PHP code for 'default value' returned @value, which is invalid.", array(
'@value' => print_r($default_value, TRUE),
)));
}
else {
form_set_error('default_value', t('The default value is invalid.'));
}
}
}
}
}