2010-04-07 2 views
2

내 zend 양식의 경우 한 달 드롭 다운하고 1 년 드롭 다운을 원합니다.Zend_Form 하나의 값으로 여러 개의 드롭 다운

서로 옆에 나타나기를 바랍니다. 그것들이 zend_form에서 분리 된 요소가되고 콘트롤러에서 그것들을 확인해도 괜찮지 만, 디자인을 위해서 나는 그들끼리 서로 옆에 앉기를 바란다.

나는 올해에 레이블을 설정하지 않고 약간의 CSS 속임수를 사용하여이를 수행 할 수 있지만 청소기 솔루션을 원합니다.

편집, 최종 코드 :

public function init($options=array()) { 

     $this->setName('add'); 
     $this->setMethod('post'); 

     $name   = new Zend_Form_Element_Text('name'); 
     $number   = new Zend_Form_Element_Text('number'); 
     $cvv   = new Zend_Form_Element_Text('cvv'); 
     $expiry_month = new Zend_Form_Element_Select('expiry_month'); 
     $expiry_year = new Zend_Form_Element_Select('expiry_year'); 
     $amount   = new Zend_Form_Element_Select('amount'); 
     $submit   = new Zend_Form_Element_Submit('Submit'); 

     $amounts  = self::load_amounts(); 
     $months   = self::load_months(); 
     $years   = self::load_years(); 

     $name->setLabel("Name On Card")->setIgnore(false)->setRequired(true); 
     $number->setLabel("Card Long Number")->setIgnore(false)->setRequired(true); 
     $cvv->setLabel("CVV2")->setIgnore(false)->setRequired(true); 
     $expiry_month->setMultiOptions($months); 
     $expiry_year->setMultiOptions($years); 
     $amount->setLabel("Amount")->setIgnore(false)->setRequired(true)->setMultiOptions($amounts); 
     $submit->setLabel("Submit")->setIgnore(true); 
     $this->addElements(array($name, $number, $cvv, $expiry_month, $expiry_year, $amount, $submit)); 
     $this->addDisplayGroup(array('expiry_month', 'expiry_year'), 'Expires'); 
    } 

그리고 만약 그 사람에게 도움이, 당신이 레이블을 설정할 수 있다고 생각하지 않습니다,하지만 당신은을 통해 FIELDSET의 제목을 설정할 수 있습니다

$this->addDisplayGroup(array('address', 'city', 'state', 'country', 'zip'), 'Address', array('legend' => 'Billing Address')); 
+0

르, 그룹에게 레이블을 제공 한숨하는 방법을 알아낼 수 없습니다! – azz0r

답변

1

다른 접근법을 사용하는 일부 코드이지만 행에 대해 설정된 레이블을 가질 수 있으며 월과 일 모두 동일한 행에 나타납니다. 또한 사용자 정의 유효성 검사 클래스와 함께 하나의 단위로 함께 유효성이 검사됩니다. 여기서 요청한 부분을 게시합니다.

$expMonth = new Zend_Form_Element_Select('exp_month'); 
$expMonth->setLabel('Card Expiry Date:') 
    ->addMultiOptions(
     array('1' => '01', '2' => '02', '3' => '03', '4' => '04 shortened') 
    ->setDescription('/'); 
$this->addElement($expMonth); 

// Generate the Expiry Year options 
$expYearOptions = array(); 
$thisYear = date('Y'); 

for ($i = 0; $i < 15; ++$i) { 
    $val = $thisYear + $i; 
    $expYearOptions[$val] = $val; 
} 


// The Expiry Year field 
$expYear = new Zend_Form_Element_Select('exp_year'); 
$expYear->removeDecorator('label') 
    ->addMultiOptions($expYearOptions) 
    ->setDescription(' (Month/Year)'); 
$this->addElement($expYear); 

// Setup Expiry Month decorators 
$expMonth->setDecorators(array(
// Show form element 
    'ViewHelper', 
    // This opens the wrapping DD tag but doesn't close it, we'll close it on 
    // the year field decorator later 
    array(array('data' => 'HtmlTag'), array('tag' => 'dd', 'id' => 'card-expire', 
     'openOnly' => true)), 
    // Using this to slip in a visual seperator "/" between both fields 
    array('Description', array('tag' => 'span', 'class' => 'seperator')), 
    // Show the label tag displayed for exp_month 
    array('Label', array('tag' => 'dt')) 
)); 

// Now for the Expiry Year field decorators 
$expYear->setDecorators(array(
    'ViewHelper', 
    // Inserting the "(Month/Year)" line using Description 
    array('Description', array('tag' => 'small', 'class' => 'greyout')), 
    // "row" is normally used to wrap a whole row, label + form element. 
    // I'm "misusing" it to close off the DD tag we opened in the month field earlier 
    // If you are already using "row", you might choose to echo the form line by line, 
    // where you close the dd tag manually like: echo 
    //  $this->form->getElement('exp_year').'</dd>'; 
    array(array('row' => 'HtmlTag'), array('tag' => 'dd', 'closeOnly' => true)) 
)); 

만료 유효성 검사 전체 코드는 내 블로그에서 확인할 수 있습니다 : http://hewmc.blogspot.com/2010/08/validating-month-and-year-fields-as-one.html

1

내가 별도의 장식을 작성하여 같은 작업을했다.

예 : 당신의 form init 함수에서이

$decorators = array('ViewHelper',array('Description',array('tag'=>'','escape'=>false)),'Errors'); 

같은 각 요소에 장식을 설정 사용 :

$name = new Zend_Form_Element_Text('name'); 

$name->setDecorators($decorators);** 

다음 줄

$this->addElements(array($name, $number, $cvv, $expiry_month, $expiry_year, $amount, $submit)); 

사용이 선 후 코드 :

$this->setDecorators (array('FormElements',array (array('data'=>'HtmlTag'), array('tag'=>'table')),'Form')); 

이제 귀하의 요구 사항과 같은 이름으로 양식에 컨트롤을 사용에 따라 PHTML을 설계

$this->element->FirstName; 
관련 문제