1

在我的用户控制器中,我有一个 editAction。以下声明来自editAction。

$form = $this->_getEditForm();//here I need to pass $user->email to the form class.

我在同一个控制器中有另一个类 _gerEditForm() 。

  private function _getEditForm()
   {
    $form = new Form_Admin_Users_Add(array(
            'method' => 'post',
            'action' => $this->_helper->url('edit', 'users'),

    ));
    return $form;
   }
enter code here

//表单类

class Form_Admin_Users_Add extends Form_Abstract
{

    public function __construct($mail=null)
    {
        parent::__construct();
        $this->setName('user');
        //$userId = new Zend_Form_Element_Text('userId');
        //$userId->addFilter('Int');
        //$this->addElement($userId);

        $this->setAttrib('id', 'addUserForm');
        $this->setAttrib('class', 'formInline');

        $firstName = new Zend_Form_Element_Text('firstName');
        $firstName->setRequired(true)
        ->addFilter('StringTrim')
        ->addValidator('NotEmpty', false, array('messages'=>'First name cannot be empty'))
        ->addValidator('StringLength', false, array(1, 256))
        ->setLabel('First Name:');
        $this->addElement($firstName);

        $lastName = new Zend_Form_Element_Text('lastName');
        $lastName->setRequired(true)
        ->addFilter('StringTrim')
        ->addValidator('NotEmpty', false, array('messages'=>'Last name cannot be empty'))
        ->addValidator('StringLength', false, array(1, 256))
        ->setLabel('Last Name:');
        $this->addElement($lastName);

        $email = new Zend_Form_Element_Text('email');
        $email->setLabel('Email :')
        ->addValidator('NotEmpty', false, array('messages'=>'email cannot be empty'))
        ->addFilter('StripTags')
        ->addFilter('StringTrim')
        ->addValidator('EmailAddress')
        ->addValidator(new BusinessForum_Validate_UniqueEmail($mail));

现在我需要将电子邮件传递给表单类。但我不知道该怎么做。请在这方面帮助我。谢谢

4

1 回答 1

1

我有点困惑。该__construct函数接受一个参数,$mail。你把它传给你的BusinessForum_Validate_UniqueEmail班级。

但是在您的_getEditForm函数中,您将一个数组传递给该$mail参数,看起来它具有表单设置,而不是电子邮件信息。

如果这就是你命名东西的方式,那就是它的工作方式,很好。然后你应该只需要在你的__construct函数中添加第二个参数:

public function __construct($mail=null, $emailAddress="")

从您的_getEditForm函数中传递它:

private function _getEditForm($emailAddress="")
{
    $form = new Form_Admin_Users_Add(array(
        'method' => 'post',
        'action' => $this->_helper->url('edit', 'users')
    ), $emailAddress);
    return $form;
}

并将电子邮件地址传递给您的_getEditForm函数:

$form = $this->_getEditForm($user->email);
于 2012-09-17T20:02:10.700 回答