2013-08-20 3 views
1

젠드 폼 요소 제출 버튼 (Zendframework1)의 기본 값을 설정하는 데 문제가 있습니다. 기본적으로 고유 한 ID 번호를 설정 한 다음 버튼이 제출되면이 번호를 검색하려고합니다.Zend_Form_Element_Submit (zendframework 1)의 값을 설정하는 방법

아래 코드는 제 코드입니다. setValue() 메서드를 사용하려고했지만 그다지 작동하지 않는다는 것을 알 수 있습니다.

$new = new Zend_Form_Element_Submit('new'); 
    $new 
    ->setDecorators($this->_buttonDecorators) 
    ->setValue($tieredPrice->sample_id) 
    ->setLabel('New'); 

    $this->addElement($new); 

또한 값을 받기 위해 사용하는 것에 대한 조언을드립니다. 즉, 값을 검색하기 위해 호출 할 메소드는 무엇입니까?

답변

1

라벨은 제출 버튼의 값으로 사용되며 제출 된 것입니다. 단추의 일부로 다른 값을 제출하는 방법이 없습니다. 제출 이름이나 값 (= 레이블)을 변경해야합니다.

대신 할 일은 숨겨진 필드를 양식에 추가하고 대신 숫자 값을 제공하는 것입니다.

+0

안녕하세요 샘. 당신의 도움을 주셔서 대단히 감사합니다. 나는 너의 충고를 따랐다. 안부 인사 Andreea – andreea115

1

이 조금 까다로운하지만, 불가능이 아니다 :

당신은 당신의 자신의보기 도우미를 구현하고 요소를 사용해야합니다.

$submit = $form->createElement('submit', 'submitElementName'); 
$submit->setAttrib('value', 'my value'); 
$submit->helper = 'customSubmit'; 
$form->addELement($submit); 
:

class View_Helper_CustomSubmit extends Zend_View_Helper_FormSubmit 
{ 
    public function customSubmit($name, $value = null, $attribs = null) 
    { 
     if(array_key_exists('value', $attribs)) { 
      $value = $attribs['value']; 
      unset($attribs['value']); 
     } 

     $info = $this->_getInfo($name, $value, $attribs); 
     extract($info); // name, value, attribs, options, listsep, disable, id 
     // check if disabled 
     $disabled = ''; 
     if ($disable) { 
      $disabled = ' disabled="disabled"'; 
     } 

     if ($id) { 
      $id = ' id="' . $this->view->escape($id) . '"'; 
     } 

     // XHTML or HTML end tag? 
     $endTag = ' />'; 
     if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) { 
      $endTag= '>'; 
     } 

     // Render the button. 
     $xhtml = '<input type="submit"' 
       . ' name="' . $this->view->escape($name) . '"' 
         . $id 
         . ' value="' . $this->view->escape($value) . '"' 
           . $disabled 
           . $this->_htmlAttribs($attribs) 
           . $endTag; 

     return $xhtml; 
    } 

} 

그래서, 당신은 요소에 도우미를 할당 :

How to add a view helper directory (zend framework)

당신의 도우미를 구현 : 처음에는

, 사용자 정의보기 도우미 경로를 추가해야합니다

이렇게하면 제출 된 값을 검색 할 수 있습니다. 형태 :

$form->getValue('submitElementName'); 
+0

내장 된'$ new-> setLabel()'메서드를 사용하는 것보다이 접근법의 장점은 무엇입니까? –

+0

맞습니다.'setAttrib ('customLabel')'대신'setLabel'을 사용하는 것이 좋습니다 (도우미에서는'$ attribs [ 'customLabel']'대신'$ this -> _ label') –

+0

안녕하세요, 알렉스와 팀. Alex 솔루션과 Tim의 후속 의견에 대해서는 완전히 혼란 스럽습니다. Alex는 사용자 정의 제출 버튼을 만드는 것이 실제로 가능하다고 말하고 있습니다. 그렇다면 customSubmit 메소드는 무엇입니까? 값을 받거나 단추를 만들기위한 것입니다. – andreea115