DonOfDen
DonOfDen

Reputation: 4088

Submit and Save action in zend form

I have two submit buttons in my ZEND FORM (zend 1.12) Lets take a scenario:

Button Submit-> Action(myproject/submit);

Button Save-> Action(myproject/save);

But the buttons are in same Form how can I make the form to post in different pages with respect to the button?

I tried the following and its not working:

//store
        $save= new Submit('save');
        $save->setLabel($this->getView()->translate('save'));
        $save->setDecorators(array('ViewHelper',array('HtmlTag', array('tag' => 'div','class'=>'btn-save')),));
        $this->setAction('/myproject/store');
        $this->addElement($save);


//store
        $submit= new Submit('submit');
        $submit->setLabel($this->getView()->translate('submit'));
        $submit->setDecorators(array('ViewHelper',array('HtmlTag', array('tag' => 'div','class'=>'btn-submit')),));
        $submit->setAction('/myproject/submit');
        $this->addElement($submit);

If I click any button it post to /myproject/submit' only!! Any help?

Upvotes: 1

Views: 2338

Answers (2)

RockyFord
RockyFord

Reputation: 8519

in zend_Form submit elements are differentiated by their label, not the name you give the element. Currently your form has 2 submit elements with the same label so they will both be represented in the $_POST array the same way. Something similar to:

array(1) {

    'submit' => 'Store'
}

You can give each submit element a unique label and then test for that label to be present in the $_POST array.

//for example
if($request->isPost()){
    if ($request->getPost('submit') == 'unique Label') {
        //do some stuff
    }
}

no javascript required...

Upvotes: 2

konradwww
konradwww

Reputation: 691

You can't setAction on Form Element. Try to use javascript and onclick /onsubmit event for both buttons instead.

Upvotes: 1

Related Questions