Per the API example, I'm trying to inject an attribute into a form. Specifically, I'm trying to add a Javascript onsubmit handler like so:

$form['#attributes']=array('onsubmit'=>'my_submit_handler();');

However, Drupal appears to be ignoring the #attributes array. Even when I use the example code of:

 $form['#attributes'] = array('class' => 'search-form');

...these attributes get ignored. I've tried injecting these changes in both hook_form and hook_form_alter. No dice. I have succeed in altering other form attributes such as #action

Any suggestions?

TIA,
dd

Comments

Steven’s picture

You're really much better off adding the handler through code rather than hardcoding it. However, it's possible #attributes is simply not supported for forms at the moment. Feel free to file a bug report.

--
If you have a problem, please search before posting a question.

drumdance’s picture

You're really much better off adding the handler through code

Could you elaborate? I AM trying to alter it in code: PHP code. Is there a better way & place for me to attach to the form? I only want to attach this on one particular form (ie I need to take a peak at $form_id in hook_form) and it seems cleaner to do it here than in Javascript on the page. Otherwise I'd have to load the JS every time and check the form tag then. Seems like a waste of bandwidth to me.

dwees’s picture

Which form are you trying to add this to? It's possible that your module has a lower weight than another module which also adjusts the form attribute, and therefore your changes are being overwritten.

Try setting the weight of your module to a higher level (you can do this by adjusting a single field in the systems table, but I'm make sure to use LIMIT 1 in your query just in case...).

Dave

drumdance’s picture

Wow, this fixed it. Thanks for the tip!

dwees’s picture

In the future, you can achieve the same effect with hook_install, and an update statement. Do a search through this site for 'adjusting weight of a module' and you'll see what I'm talking about.

Obviously this is only if you intend to release your module to the general public.

Dave

drumdance’s picture

After playing around with it a little more (and actually after introducing a new bug as a result), I've found that this plays nice with existing form attributes that may have been injected:

$form['#attributes']['name of attribute']='value';

The following sentence is true.
The preceding sentence is false.
http://www.AskDerekScruggs.com

carligraph’s picture

hi, i was trying your solution, but dont know exactly how to do.
for example i changed the 'weight' field for (module) 'content' in table 'systems' to 20, which that didnt help).
what field do i have to change now, if i wanna inject $form['field_select_nodereference']['#attributes'] in a cck node-edit template ?
can anyone help me out there ? thanx!

brollo’s picture

I tried this on drupal 5.2 and it didnt work..

I also tried other ways, cant get it, any ideas?

Thanks

nevets’s picture

It would help to see the code you tried in order to help you.

brollo’s picture

No I had tried with the exact same syntax but it didnt work...

I managed to make it work though later with a similar, but not exactly the same syntax.. and eventually it did..

Thanks anyways