2010-04-07 21 views
2

對於我的zend表單,我希望一個月下降,一年下降。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'); 
    } 

,如果它有幫助的人,我不認爲你可以設置標籤,但你可以通過設置一個字段標題:

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

無法弄清楚如何給一組標籤,樂感嘆! – azz0r 2010-04-07 09:54:21

回答

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; 
相關問題