【问题标题】:Zend_Form Multiple dropdown as one valueZend_Form 多个下拉列表作为一个值
【发布时间】:2010-04-07 09:16:49
【问题描述】:

对于我的 zend 表单,我想要一个月下拉和一年下拉。

我希望它们彼此相邻出现。可以将它们作为 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'));

【问题讨论】:

  • 无法弄清楚如何给一个组一个标签,le sigh!

标签: zend-form


【解决方案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

【讨论】:

    【解决方案2】:

    我通过创建一个单独的装饰器来完成同样的任务。

    例如:在你的 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;
    

    【讨论】:

      猜你喜欢
      • 2021-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多