Zend_Form多个下拉列表作为一个值

时间:2010-04-07 09:16:49

标签: 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'));

2 个答案:

答案 0 :(得分: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 :(得分: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;