开发者

Changing Zend_Form behavior to call JS function upon submit

开发者 https://www.devze.com 2023-01-18 10:09 出处:网络
I would like to change the default behavior of Zend_Form, so that whenever the submit button is clicked, form submission is prevente开发者_高级运维d and an arbitrary JavaScript function is called. Thi

I would like to change the default behavior of Zend_Form, so that whenever the submit button is clicked, form submission is prevente开发者_高级运维d and an arbitrary JavaScript function is called. This would be done for all forms on the site, however the actual JS function to call may change from form to form.

What would be the most proper way to do this? Through a custom decorator on the submit element? Could you provide some tips?

Thanks.


In my opinion, Zend_Form shouldn't need or desire any knowledge of the JavaScript. I would add a class to the form and use that class name as your hook for applying javascript.

Imagine a form:

<form class="validate-me">
  <!-- //elements -->
  <input type="submit">
</form> 

You can then apply your JavaScript to forms with a given class name (pseudo JS, use a library)

var el = document.getElementsByClassName('validate-me')[0];
el.on('submit', function(e){
  //validate the form, stop the event if invalid
});

If you want to add extra markup or attributes instead of this approach then you will certainly need to look at decorators.

0

精彩评论

暂无评论...
验证码 换一张
取 消