1

I have:

{{ form_start(form, {'action': path('admin_user_edit_exec',{'user_id':id,'babis':'777'}),'attr': {'class': 'smart-form userform','id':'formid'} } ) }}

I want to add smthing like: onsubmit:"return validateForm()"
to call a js function but struggling with syntax,everywhere tried to put the onsubmit.

Form stopped working, where i should put it?

Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85
Giorgos Nikolaidhs
  • 71
  • 1
  • 2
  • 11
  • jquery $(document).ready(function(){ $('#yourform').on('submit', ...); }); – Aitch Feb 08 '15 at 00:25
  • 2
    after your twig is compiled into html you can do what you want. Why should there be any problems? What is 'stopped working'? Did you check the dev console of JS? any errors? instead of showing twig, show the html code. Do you use any magic form widgets with JS, maybe some conflicts? – Aitch Feb 08 '15 at 01:00
  • I agree with @Aitch. Do try to separate `Twig` from `JS` as much as possible. – Jovan Perovic Feb 08 '15 at 08:03
  • Aitch when i said "same result" was meaning your jquery makes what i was trying to..its working ok thnx – Giorgos Nikolaidhs Feb 08 '15 at 20:58

1 Answers1

7

There's no problem about rendering that attribute in the html tag. You can do it the same way you'd do it with any other attribute (class, id ...).

For instance (I've changed the action a bit to adapt it to my SF installation):

{{ form_start(form, {'action': path('_demo_hello',{'name':5}),'attr': {'class': 'smart-form userform','id':'formid', 'onsubmit':'return validateForm();'} } ) }}

would render:

<form name="contact" method="post" action="/symfony/web/app_dev.php/demo/hello/5" class="smart-form userform" id="formid" onsubmit="return validateForm();">

If you have the js function validateForm well set up, it should work. Anyway, if it didn't, that would be a problem related to your js and not to SF.

As pointed out in the comments, it'd be better practice to attach the submit event to the element using js instead of adding it in the HTML tag. You can do that using either jQuery or pure js (which is pretty straightforward too, no need to rely on jQuery always).

Community
  • 1
  • 1
acontell
  • 6,792
  • 1
  • 19
  • 32