0

I used Ajax in a form so that submission of form does not lead to reload of the page.It is working . But the problem is 'The Validation code is not working'. This is the code I have used in the form.

This is the code for submit button.

$form['submit'] = array(
  '#type' => 'submit',
  '#ajax' => array(
    'callback' => 'submit_driven_callback',
    'wrapper' => 'myform',
    'method' => 'replace',
    'effect' => 'fade',
  ),
  '#value' => t('Submit'),
);

I have put the validation part in the following function.

function formname_validate($form, &$form_state) {
  // Validation code
}

Am I doing it the wrong way? Should I put the validation part somewhere else?

4
  • Yours function name should be named like this: modulename_formname_validate
    – Codium
    Commented Jul 5, 2012 at 18:13
  • I tried doing that , but it is still not working . Commented Jul 5, 2012 at 18:24
  • this is my code function ajaxform_ajax_myform_validate($form, &$form_state) { if (!valid_email_address($form_state['values']['emailid'])) { form_set_error('emailid', t("The email address you supplied is not valid.")); }} Commented Jul 5, 2012 at 18:25
  • Try removing the submit callback and see if your validation callbacks now work. I'm having a similar error where it looks like validation doesn't work but the real problem is that the submit callack gets called regardless.
    – Alex Weber
    Commented Jul 27, 2012 at 5:04

2 Answers 2

1

You can try the following code.

$form['submit']['#validate'] = array('YOUR_VALIDATE_FUNCTION_NAME');
1

I am working with AJAX code nowadays, and as far as I can see, if the form builder is formname(), then formname_validate() should work as form validation handler.

1
  • should. doesn't. Commented Dec 13, 2017 at 16:58

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.