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?

  • Yours function name should be named like this: modulename_formname_validate
    – Codium
    Jul 5 '12 at 18:13
  • I tried doing that , but it is still not working . Jul 5 '12 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.")); }} Jul 5 '12 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
    Jul 27 '12 at 5:04

You can try the following code.

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

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.

  • should. doesn't. Dec 13 '17 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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