在Zend Framework 1.10.8中使用displayGroup时,提交按钮落在fieldset之外

时间:2010-09-26 21:12:00

标签: zend-framework zend-form form-submit zend-form-element

我已经尝试了所有可以考虑的组合,但这种形式:

class Form_Login Extends Zend_Form{

    public function init(){
        $this->setMethod('post');


        $email = new Zend_Form_Element_Text('email');
        $emailValidator = new Zend_Validate_EmailAddress();
        $emailValidator->setMessage('Please use a valid email address');
        $email->addValidator($emailValidator)
                ->setRequired(TRUE)
                ->setLabel("Email Address")
                ->setAttrib('size', '35');

        $password = new Zend_Form_Element_Password('password');
        $password->setLabel('Password')
                ->setAttrib('size', '35')
                ->setRequired(true);

        $submit = new Zend_Form_Element_Submit('login');
        $submit->setLabel('Login');


        $this->addElements(array(
            $email,
            $password
            )
        );



        $this->addDisplayGroup(array(
                    'email',
                    'password'

        ),'loginGroup',array('legend' => 'Login'));


        $loginGroup = $this->getDisplayGroup('loginGroup');


        $this->setElementDecorators(array(
            'ViewHelper',
            array('Label', array('separator' => '', 'requiredPrefix' => '* ')),
            array('Errors'),
            array('HtmlTag', array('tag' => 'p', 'class' => 'form-element')),
        ));


        $loginGroup->setDecorators(
            array(
                'FormElements',
                 array('HtmlTag',array('tag'=>'div','openOnly'=>true)),
                 'Fieldset'
                )
        );


        $this->addElements(array($submit));

        //buttons do not need labels
        $submit->setDecorators(array(
            array('ViewHelper'),
            array('HtmlTag', array('tag' => 'p', 'class' => 'submit-button'))
        ));

    }
}

在Google Chrome中显示效果如下图所示:

<fieldset id="fieldset-loginGroup"><legend>Login</legend>
<div>
<p class="form-element"><label for="email" class="required">* Email Address</label>
<input type="text" name="email" id="email" value="" size="35"></p>
<p class="form-element"><label for="password" class="required">* Password</label>
<input type="password" name="password" id="password" value="" size="35"></p>
<p class="submit-button">
<input type="submit" name="login" id="login" value="Login"></p></div>
</fieldset>

在firefox中,它不属于fieldset标签所以它看起来像这样:

<dl class="zend_form">
<fieldset id="fieldset-loginGroup"><legend>Login</legend>
<div>
<p class="form-element"><label for="email" class="required">* Email Address</label>
<input name="email" id="email" value="" size="35" type="text"></p>
<p class="form-element"><label for="password" class="required">* Password</label>
<input name="password" id="password" value="" size="35" type="password"></p></div>
</fieldset>
<p class="submit-button">
<input name="login" id="login" value="Login" type="submit"></p></dl>

我知道浏览器不应该产生很大的不同,因为标签是在服务器端生成的。但是我不能让提交按钮落在firefox中设置的字段中。除了这两个之外,我还没有尝试过任何其他浏览器,所以我不确定它们会如何看待它们。任何帮助......?

1 个答案:

答案 0 :(得分:0)

绕过这个我试过了:

class Form_Login Extends Zend_Form{

public function init(){
    $this->setMethod('post');

    $email = new Zend_Form_Element_Text('email');
    $emailValidator = new Zend_Validate_EmailAddress();
    $emailValidator->setMessage('Please use a valid email address');
    $email->addValidator($emailValidator)
            ->setRequired(TRUE)
            ->setLabel("Email Address")
            ->setAttrib('size', '35');

    $password = new Zend_Form_Element_Password('password');
    $password->setLabel('Password')
            ->setAttrib('size', '35')
            ->setRequired(true);

    $submit = new Zend_Form_Element_Submit('login');
    $submit->setLabel('Login');


    $this->addElements(array(
        $email,
        $password,
        $submit
        )
    );


    $this->setLegend('Login');
    $this->setElementDecorators(array(
        'ViewHelper',
        array('Label', array('separator' => '', 'requiredPrefix' => '* ')),
        array('Errors'),
        array('HtmlTag', array('tag' => 'p', 'class' => 'form-element')),
    ));

    $this->setDecorators(array(
        'FormElements',
        'Fieldset', 
        'Form'
        ));

    //buttons do not need labels
    $submit->setDecorators(array(
        array('ViewHelper'),
        array('HtmlTag', array('tag' => 'p', 'class' => 'submit-button'))
    ));

}

}

现在它显示得很好。区别在于:

$this->setDecorators(array(
        'FormElements',
        'Fieldset', 
        'Form'
        ));

并将图例设置为:

$this->setLegend('Login');