开发者

Grails One form can have two actions?

开发者 https://www.devze.com 2023-03-22 18:46 出处:网络
my form has got two buttons ... both for entirely different purpose.. so there is a necessity to have sep开发者_StackOverflow中文版arate actions ... Is it possible? if not what is the best possible so

my form has got two buttons ... both for entirely different purpose.. so there is a necessity to have sep开发者_StackOverflow中文版arate actions ... Is it possible? if not what is the best possible solution to deal with such problem... Thanks is advance


The <g:actionSubmit> tag allows you to have multiple submit buttons in a single form.

<g:form controller="test">
  <g:actionSubmit value="Submit to success" action="success"/>
  <g:actionSubmit value="Submit to delete" action="delete"/>
</g:form>


You can use <g:actionSubmit/> buttons to have a single grails form go to different actions.

<g:form method="post">
    ...
    <g:actionSubmit action="oneAction" value="One Action"/>
    <g:actionSubmit action="anotherAction" value="Another Action"/>
</g:form>

Clicking "One Action" would be handled by the oneAction action of the current controller; clicking "Another Action" would be handled by the anotherAction action of the current controller.


A single form can have only one action.

You can change that action, dynamically, using JavaScript, but that adds a dependancy on JS.

Better is to have the form submit to a single URL and then do something along the lines of (pseudo code):

if (submit is foo) {
    process_form_with_foo_class()
} elseif (submit is bar) {
    process_form_with_bar_class()
} else {
    // Form was submitted without using a submit button
    // e.g. with enter, or JS.
    process_form_with_foo_class() // Let's use foo as the default
}
0

精彩评论

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

关注公众号