How can I validate the user input in order to avoid certain strings as nodetitles?
Say I do not want to allow the user to create nodetitles that contain a comma … is there any way i can trigger a form_set_error() when the form is being validated?

Comments

tinker’s picture

You can do this by making your own module that alters the form. Its not easy if you are not a programmer. Replace anything beginning with "my" using the names you select for the module, content type, and field.

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'mycontenttype_node_form':
      $form['#validate'][] = 'mymodule_mycontenttype_form_validate';
      break;
  }
}

function mymodule_mycontenttype_form_validate ($form, &$form_state) {
  if (strstr($form_state['values']['myfield']['value'], ",")) {
    form_set_error('myfield', t('This field cannot have commas'));
    $form_state['rebuild'] = TRUE;
  }
}