开发者

Zend two submit buttons same name

I built a form class extending zend_form.I have two submitting buttons :

$like=new Zend_Form_Element_Image('vote');
$like->setImage($config->path->images."up.png")
->setValue(2);

$dislike=new Zend_Form_Element_Image('vote');
$dislike->setImage($config->path->images."down.png")
->setValue(1);

I'd want to have two submit buttons with same name and different value so that I can later access the value of the submitted one by a switch statement in the controller:

$submit = $this->_request->getPost('vote');

 switch($submit) {
 case 'one':
 // button one was pressed
 break;
 case 'two':
}

}
开发者_高级运维

But If I do set those with the same name the last one is overriding the first so only one button is output.

With file elements theres a method setMultifile() that does the trick.

what should I do here?

thanks

Luca


You might try using array notation for the images.


Why not do something like this:

if( $this->_request->isPost() && $yourForm->isValid( $this->_request->getPost() ) ) {
    if($yourForm->one->isChecked())
       // button one was pressed
    elseif($yourForm->two->isChecked())
       // button two was pressed
}


As you have stated second button with the same name will overide the first. The only solution I can think of is to use a viewScript for the form and add the button HTML manually.

I hope this helps.

Regards,

Garry


It's possible. What you would do is you would need each submit button to belong to a different subform or fieldset within the main form.

In that way the submit buttons would then be namespaced, and you will be able to add them both to the form.

Each submit button will then have the same name. However the fully-qualified name of each submit button will still be different.

E.g.

$likeSubform = new Zend_SubForm();
$dislikeSubform = new Zend_SubForm();

$like=new Zend_Form_Element_Image('vote');
$like->setImage($config->path->images."up.png")
     ->setValue(2);

$dislike=new Zend_Form_Element_Image('vote');
$dislike->setImage($config->path->images."down.png")
        ->setValue(1);

$likeSubform->addElement($like);
$dislikeSubform->addElement($dislike);

$form->addSubForm($likeSubform, 'like');
$form->addSubForm($dislikeSubform, 'dislike');

//===================================================

echo $form->getSubForm('like')->vote->getName();

//vote

echo $form->getSubForm('like')->vote->getFullyQualifiedName();

//like[vote]

Unfortunately for you, later when you render the form, the ViewHelper decorator that ships with ZF is going to render the submit button using the fully-qualified name. (assuming you use that decorator, which most people do)

However you could then create your own ViewHelper decorator that overrides the default, thus rendering the form element using the unqualified name instead:

class My_Form_Decorator_ViewHelper extends Zend_Form_Decorator_ViewHelper
{

    //override default render method
    public function render($content)
    {
        ...

        $name = $element->getName();
        $id   = $element->getName();

        ...
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜