使用Zend Framework 1.12创建一个包含单选按钮的表单?

时间:2015-05-05 13:42:18

标签: php forms zend-framework

我使用Zend Framework 1.12,我正在尝试使用Zend_Form创建一个表单:

<form action="">
<div class="bb-validator-form">
    <div class="form-question">
        Question 1?
    </div>
    <div class="form-row" >
        <input type="radio" name="form-val-q" id="yes" class="radio radio-yes"/>
        <label for="yes"><?php echo $this->tra->_('Yes'); ?></label>
    </div>

    <div class="form-row">
        <input type="radio" name="form-val-q" id="no" class="radio radio-no"/>
        <label for="no"><?php echo $this->tra->_('No'); ?></label>
    </div>
    <input type="submit" class="btn btn-primary btn-block" value="<?php echo $this->tra->_('Validate'); ?>"/>
</div>

我在Zend Form中的代码是:

class Form_BB_Validator extends Zend_Form {
    public function init()
    {
        $this->setMethod(Zend_Form::METHOD_POST)
            ->setAttribs(array('id' =>'bb-Form'))
            ->addDecorator('FormElements')
            ->addDecorator('HtmlTag', array('tag' => 'div','class'=>'bb-validator-form'))
            ->addDecorator('Form');

        $this->addElement('text','question-1',array(
            'label'         => 'test',
            'decorators'=>array(array('Callback',array('callback'=>
                                                           create_function('','return "<h4> Question 1 ?</h4>";'))))
        ));

        $this->addElement('radio','checkValidation', array(
            'class'         => 'radio',
            'escape'        => false,
            'required'      => true,
            'multioptions'  => array('yes'=>'Yes', 'no'=>'No'),
            'decorators'    => array(
                                    array('FormElements',  array('HtmlTag', array('tag'=>'div','class'=>'bb-validator-form'))),
                                    array('ViewHelper',  array('Label', array('tag'=>'div','class'=>'bb-validator-form'))),
                                    )
        ));

        $this->addElement(new Zend_Form_Element_Submit('submit', array(
            'label'         => 'Valider',
            'class'         => 'btn btn-primary btn-block',
            'decorators'    => array(array('ViewHelper',  array('Label', array('tag'=>'div','class'=>'bb-validator-form'))),

            ))));
    }
}

最终输出是:

<form id="bb-Form" enctype="application/x-www-form-urlencoded" action="" method="post">
<div class="bb-validator-form">
    <h4>Question 1 ?</h4>
    <dt id="checkValidation-label">&nbsp;</dt>
    <label for="checkValidation-yes">
    <input type="radio" name="checkValidation" id="checkValidation-yes" value="yes" class="radio">Oui</label>
    <br>
    <label for="checkValidation-no">
    <input type="radio" name="checkValidation" id="checkValidation-no" value="no" class="radio">Non</label>
    <input type="submit" name="submit" id="submit" value="Valider" class="btn btn-primary btn-block">
</div>
</form>

这不正确。如何像第一段代码一样创建表单?

1 个答案:

答案 0 :(得分:1)

您可以使用自定义viewScript。像这样:

public class HandicapRecords {

    private Map<Integer, Set<String>> handicapMap;

    public HandicapRecords() {
        handicapMap = new HashMap<>();
    }

    public void handicapMap() {
        Set<String> players = new HashSet<>();

        players.add("Michael");
        players.add("Roger"); 
        players.add("Toby");
        handicapMap.put(10, players);

        players = new HashSet<>();
        players.add("Bethany");
        players.add("Martin");
        handicapMap.put(16, players);

        players = new HashSet<>();
        players.add("Megan");
        players.add("Declan");
        handicapMap.put(4, players);
    }

    public void printMap() {
        //code for method to go here
    }

}

在form-b​​b-validator.phtml中,你可以像这样设计你的html输出:

$this->setDecorators(
            array(
                array(
                    'ViewScript',
                    array(
                        'viewScript' => $viewsPath . '/form-bb-validator.phtml'
                    )
                )
            )
        );