0

I have altered the registration form to change the thank you message and add the validation of some fields.

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_register_form') {
    $form['actions']['submit']['#validate'][]  = 'checkout_nationality_form_validate';
    $form['#submit'][] = 'thank_on_submit';
  }

All code is working fine; my problem is the registration doesn't take the default Drupal validation. Why does this happen?

0

2 Answers 2

5

You need to change this line

 $form['actions']['submit']['#validate'][]  = 'checkout_nationality_form_validate';

to

 $form['#validate'][] = 'checkout_nationality_form_validate';
0
4

Your custom validation function should go into the $form['#validate'] array

$form['#validate'] = 'checkout_nationality_form_validate';

I prefer using the implement hook_form_FORM_ID_alter instead of using an if statement in the hook_form.

/**
 * Implements hook_form_form_id_alter().
 */
function MYMODULE_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  $form['#validate'][] = 'checkout_nationality_form_validate';
}

/**
 * Custom validation function.
 */
function checkout_nationality_form_validate(&$form, &$form_state) {
  if($form_state['value']['my_field'] == 'is not valid') {
    form_set_error('my_field', t('Value not valid'));
  }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.