I have added a few extra fields to the node_type_form form and would like to add some validation. My validation hook is being called, but if it fails, it still returns back to the "admin/content/types" page, with my error at the top. I do not want the form to process and i want the user to stay on the page until the form validates correctly.

I tried adding $form['#validate'] = 'myvalidationhook_validate';

and that didn't work.

I also found a blog post which said to try something a little bit different (http://anandselvamca.wordpress.com/2010/04/28/adding-custom-validation-t...) but that didn't work for me either.

Please let me know if I'm doing anything wrong.

Here's my code:

function socialtoaster_content_profile_form_alter(&$form, $form_state, $form_id) {
global $base_url;

// Only show SocialToaster form fields if the person can administer SocialToaster
if (user_access('administer SocialToaster')) {

if ($form_id == 'node_type_form') {

$type = $form['#node_type'];
$fields = content_types($type->type);
$fields = (array)$fields['fields'];
$options = array('' => t('Post All Content'));
foreach ($fields as $field_name => $field) {
if ($field['type'] == 'text') {
$options[$field_name] = $field['widget']['label'];
}
}

$form['socialtoaster']['socialtoaster_content_profile_post_field'] = array(
'#type' => 'select',
'#title' => t('Content Field'),
'#options' => $options,
'#description' => t('The selected field will have to match the profile field in order for content to post through SocialToaster.'),
'#default_value' => variable_get('socialtoaster_content_profile_post_field_'. $form['#node_type']->type, '')
);

$profile_options = array('' => t('Post All Content'));
$profile_types = content_profile_get_types();
foreach ($profile_types as $profile_type) {
$profile_fields = content_types($profile_type->type);
$profile_fields = (array)$profile_fields['fields'];
foreach ($profile_fields as $profile_field_name => $profile_field) {
if ($profile_field['type'] == 'text') {
$profile_options[$profile_type->type . "." . $profile_field_name] = $profile_type->name . ": " . $profile_field['widget']['label'];
}
}
}

$form['socialtoaster']['socialtoaster_content_profile_profile_field'] = array(
'#type' => 'select',
'#title' => t('Profile Field'),
'#options' => $profile_options,
'#description' => t('The selected field will have to match the content field in order for content to post through SocialToaster.'),
'#default_value' => variable_get('socialtoaster_content_profile_profile_field_'. $form['#node_type']->type, '')
);

//$form['#validate'][] = 'socialtoaster_content_profile_node_type_form_submit';
$form['#validate'][] = array_merge(array('socialtoaster_content_profile_node_type_form_validate' => array()), $form['#validate']);
$form['#submit'][] = 'socialtoaster_content_profile_node_type_form_validate';

}

}

}

function socialtoaster_content_profile_node_type_form_validate($form, &$form_state) {

if ($form_state['values']['socialtoaster']['socialtoaster_content_profile_post_field'] == '' && $form_state['values']['socialtoaster']['socialtoaster_content_profile_profile_f$
form_set_error('socialtoaster_content_profile_post_field', t('Please choose a content match field for SocialToaster.'));
}

if ($form_state['values']['socialtoaster']['socialtoaster_content_profile_post_field'] != '' && $form_state['values']['socialtoaster']['socialtoaster_content_profile_profile_f$
form_set_error('socialtoaster_content_profile_profile_field', t('Please choose a profile match field for SocialToaster.'));
}

}

Comments

Status: Active » Closed (outdated)

Automatically closed because Drupal 6 is no longer supported. If the issue verifiably applies to later versions, please reopen with details and update the version.