views:

616

answers:

1

Having multiple forms in one page, when i submit one of them, how can i tell wich one was submitted?

I thought about generating uniqe ids for each from, and saving them as hidden fields and to the user-session - while this is a solution, the problem with it is that there is no good place to remove old ids from the session.

Any better ideas how to solve this problem?

Thanks in advance!

+1  A: 

First of all: have you considered sending the two forms to two different actions? That way you can handle each form separately in an action each. This should be the "best-pratice" if you're using the Zend MVC component.

The other option is to check for the value of the submit button which will be included in the request, e.g.

<input type="submit" name="save" value="form1" />
// in PHP:
// $_POST["save"] will contain "form1"

<input type="submit" name="save" value="form2" />
// in PHP:
// $_POST["save"] will contain "form2"

Be careful as the value-attribute will be rendered as the button's label.

So perhaps you want to distingush the forms by different submit-button names:

<input type="submit" name="save-form1" value="Submit" />
// in PHP:
// $_POST["save-form1"] will contain "Submit"

<input type="submit" name="save-form2" value="Submit" />
// in PHP:
// $_POST["save-form2"] will contain "Submit"

EDIT:

During the comment-dialog between the OP and myself the following seems to be a possible solution:

class My_Form_Base extends Zend_Form
{
    private static $_instanceCounter = 0;

    public function __construct($options = null)
    {
        parent:: __construct($options);

        self::$_instanceCounter++;
        $this->addElement('hidden', 'form-id', 
            sprintf('form-%s-instance-%d', $this->_getFormType(), self::$_instanceCounter);
    }

    protected _getFormType()
    {
        return get_class($this);
    }
}

class My_Form_Type1 extends My_Form_Base
{
    public function init()
    {
        // more form initialization
    }
}

class My_Form_Type2 extends My_Form_Base
{
    public function init()
    {
        // more form initialization
    }
}
Stefan Gehrig
Two Actions: can't really do that because i want to display the form + errors in the same page if it does not validate. Submit Button: There can be multiple instaces of the same form in one page. Additionally i do not want to pass a parameter to the form on creation. this would be tooo easy ;)
smoove666
I think you should describe what you're trying to achieve in more detail. You want to generate a page with several forms (some of them instances of the same form), you don't want to add anything special to each form but you nevertheless want to be able to differiate the forms on submit?
Stefan Gehrig
And I don't see why posting to different actions won't be a solution... You can render whatever you want from each of those actions.
Stefan Gehrig
S. Gehring: 1st comment: that is exactly what i want. 2nd comment: So imagine this: The number of the forms in that page is not defined, it can be one, it can be 50, so there is no way i can give each one a separate action.
smoove666
Im asking for a general solution for this problem, not one that works for the moment.
smoove666
Ah ok - I though we're talking about 2 forms. 50 forms should be something different. Can you explain why adding a hidden field with some kind of form-id (e.g. "form-1-instance-2") isn't a solution?
Stefan Gehrig
that would be a great solution, but how would i automate the "instance-2" part? :D
smoove666
While i could implement a static counter variable in Zend_Form, this would by quiet risky, imagine this situation: i have a list of 50 entries, each has its own form. the page loads, every form gets its id. now before i submit one of the forms, the entries change, for example one entry gets deleted (this means one form less somewhere in the middle), now the ids do not match anymore.
smoove666
I just edited my answer but you requirement to bind forms to specific model instances makes the static counter solution I added above obsolete.
Stefan Gehrig
Couldn't you just replace the instance counter with the id of the underlying model entity when filling the form with values from your model? How do you pass the model entity to your forms?
Stefan Gehrig
Yes, i had that static counter idea too, but i really need a rock solid solution.
smoove666
in this particular case there really is no model, imagine the forms as recommend-forms where a user just enters an emailadress, i then add a hidden field with an id referencing the entry. But as is sayd, i need a general solution, that will work all the time, completely independant form what kind of form i have, or what data gets set in it.
smoove666
I think this won't be possible... I'll try to put it this way: you're trying to couple your form to some entity of which you know nothing. There exists not even a common basic contract between the entity and the form or a common assumption on the entity. I'd say that this cannot be done with the required generality.
Stefan Gehrig
Thanks for your efforts, if i ever find a solution i will let you know :)
smoove666