3

I created a simple form. How could I add JavaScript commands after form was validated? Why is it so hard do this?

$form['submit'] = array(
  '#type' => 'submit',
  '#value' => 'submit',
  '#validate' => 'my_validate_callback',
  '#ajax' => array(
    'callback' => 'my_form_callback',
    'method' => 'replace',
    'wrapper' => 'form-wrap',
    'effect' => 'fade'
  )
);
2
  • Could you be more descriptive of the problem. Why add js only after form is validated. What are you trying to do? Commented Aug 1, 2015 at 18:52
  • hello, i'm trying run a fancybox command when validate return some error, for open a div that it's a popup. Commented Aug 2, 2015 at 19:52

1 Answer 1

0

If you only want to add a JS file in validate or Ajax handler function you just need to add JS to form with drupal_add_js or attach it to form.

This is an example of how can you add JavaScript in Ajax callback function:

function my_form_callback(&$form,$form_state){
if(some validation ){
$form['#attached']['js'] = array(
  drupal_get_path('module', 'ajax_example') . '/ajax_example.js',
);
}
return $form;
}
1
  • I want execute a javascript when validate callback return some errors and when all it's okay. if i use $form['#prefix'] = '<script>...</script'; ??? Commented Aug 2, 2015 at 19:58

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.