function _content_admin_field_validate in Content Construction Kit (CCK) 6
Same name and namespace in other branches
- 5 content_admin.inc \_content_admin_field_validate()
Validate a field's settings.
File
- includes/
content.admin.inc, line 825 - Administrative interface for content type creation.
Code
function _content_admin_field_validate($form, &$form_state) {
include_once './' . drupal_get_path('module', 'content') . '/includes/content.crud.inc';
$form_values = $form_state['values'];
$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 (isset($form_values['default_value_php']) && ($php = trim($form_values['default_value_php']))) {
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] = "'{$column}' => value for {$column}";
}
$sample = 'array(
0 => array(' . implode(', ', $columns) . '),
// You\'ll usually want to stop here. Provide more values
// if you want your \'default value\' to be multi-valued :
1 => array(' . implode(', ', $columns) . '),
2 => ...
);';
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';
// 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, NULL, NULL);
}
// The field validation routine won't set an error on the right field,
// so set it here.
if (form_get_errors()) {
if (trim($form_values['default_value_php'])) {
form_set_error('default_value_php', t('The default value PHP code created @value which is invalid.', array(
'@value' => print_r($default_value, true),
)));
}
else {
form_set_error('default_value', t('The default value is invalid.'));
}
}
}
}
}