1

アプリケーションに複数のZendFramework(v1.12)フォームがあります。

メインフォーム:

<?php

class Application_Form_Main extends Zend_Form
{

    public function init()
    {
        $this->setMethod('post')->setAction('some/url');
    }
}

?>

私のサブフォーム:

<?php

class Application_Form_User extends Zend_Form_SubForm
{

    public function init()
    {

       //first name element
        $this->addElement('text', 
                          'first_name', 
                          array(
                            'label'      => 'Name',
                            'required'   => true,  
                            'filters'    => array('StringTrim')           
                          )
        );

        //last name element
        $this->addElement('text', 
                          'last_name', 
                          array(
                            'label'      => 'Surname',
                            'required'   => true,
                            'filters'    => array('StringTrim')           
                          )
        );

        $this->setElementDecorators(array(
            'ViewHelper',
            'Errors'
        )); 
    }    

}

?>

私のカスタムコントローラー(たとえば、UsersController.php)では、メインフォームを複数のユーザーサブフォームでレンダリングしています。

<?php 

$mainForm = new Application_Form_Main();

for($i=0; $i<2; $i++){

    $userForm = new Application_Form_User();              

    $mainForm->addSubForm($userForm, 'user_'.($i+1)); 

}       

//passing main form to the template
$this->view->mainForm = $mainForm;

?>

したがって、2人のユーザーのfirst_nameフィールドとlast_nameフィールドを持つフォームを取得しています。

私のテンプレートでは、Imレンダリングフォームは次のようになっています。

<form action="<?php echo $this->mainForm->getAction(); ?>"
      enctype="<?php echo $this->form->getEnctype(); ?>"
      method="<?php echo $this->form->getMethod(); ?>"
">

<?php echo $this->mainForm->getSubForm('user_1')->first_name; ?>
<?php echo $this->mainForm->getSubForm('user_1')->last_name; ?>

<?php echo $this->echo $this->mainForm->getSubForm('user_2')->first_name; ?>
<?php echo $this->echo $this->mainForm->getSubForm('user_2')->last_name; ?>

</form>

問題は、first_nameとlast_nameのテキストフィールド名が両方の形式で同一であるということです。どうすれば一意の名前を付けることができますか?フォームを出力した場合:

<?php echo $this->mainForm; ?>

その後、すべてが大丈夫です、私は異なるフィールド名を取得します。

それで、何かアイデアはありますか?

4

0 に答える 0