Zend two buttons to send the same name

I built a form class extending zend_form.I have two submit 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 would like to have two submit buttons with the same name and a different value, so that I can later access the value represented using the switch statement in the controller:

 $submit = $this->_request->getPost('vote'); switch($submit) { case 'one': // button one was pressed break; case 'two': } } 

But if I install those with the same name, the latter overrides the first, so only one button is displayed.

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

what should i do here?

thanks

Luke

+6
source share
4 answers

You can try using array notation for images.

+2
source

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 } 
0
source

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

Hope this helps.

Hi,

Garry

0
source

It is possible. What would you do is what you need for each submit button to belong to a different subform or set of fields in the main form.

This way, the submit buttons will then be placed in the namespace, and you can add them to the form.

Each submit button will have the same name. However, the full name of each submit button will still be different.

eg.

 $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 comes with ZF will display the submit button using the full name. (assuming you are using this decorator, which most people do)

However, you could create your own ViewHelper decorator that overrides the default value, so instead an unqualified name will be used instead of a form element:

 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
source

Source: https://habr.com/ru/post/889608/


All Articles