Zend表单和URL参数

时间:2013-01-11 15:37:35

标签: php forms zend-framework parameters

我有一个Zend表单和此表单的URL。

网址有3个参数:firstname,lastname和email。 在我的表单中,我有3个输入字段,也有名字,姓氏和电子邮件。

每当我提交表格时。它从URL参数中获取值而不是表单输入字段。

有没有办法在表单中获取值?

我知道url中的参数与表单输入字段*

的名称相同

网址:http://mywebsite.com/thank-you?key=e72671a8640b45c2401afe887b9b530a& first_name =第一个& last_name =用户& 电子邮件 =user_3fdfsdfs@gmail.com

形式的Zend动作控制器:

$signupForm = new Application_Form_UserSignUp();
if ($signupForm->isValid($this->getRequest()->getParams()))
        {

            $user = $this->_helper->model('Users')->createRow($signupForm->getValues());   
            if ($user->save())
            {
                Zend_Session::rememberMe(186400 * 14);
                Zend_Auth::getInstance()->getStorage()->write($user);
                $user->sendSignUpEmail();
                $this->getHelper('redirector')->gotoRoute(array(), 'invite');
                return;
            }
        }
$this->view->signupForm = $signupForm;

Zend表格:

class Application_Form_UserSignUp extends Zend_Form
{
public $first_name, $last_name, $email, $submitButton;

public function init()
{
    $this->setName('signupForm'); 

    $this->first_name = $this->createElement('text', 'first_name')->setRequired(true);
    $this->last_name = $this->createElement('text', 'last_name')->setRequired(true);

    // Check if email is duplicated in database
    $noEmailExists = new Zend_Validate_Db_NoRecordExists(
            array(
                'table' => 'users',
                'field' => 'email'
            )
        );
    $noEmailExists->setMessage('%value% is already used by another person, please try again with different email address', Zend_Validate_Db_Abstract::ERROR_RECORD_FOUND);

    $this->email = $this->createElement('text', 'email')
                     ->setLabel('Email')
                     ->addValidator($noEmailExists)
                     ->addValidator('EmailAddress')
                     ->setRequired(true);

    $this->submitButton = $this->createElement('button', 'save')
                            ->setLabel('Sign Up')
                            ->setAttrib('type', 'submit');

    $this->addElements(array($this->first_name, $this->last_name, $this->email, $this->submitButton));

    $elementDecorators = array(
        'ViewHelper'
    );
    $this->setElementDecorators($elementDecorators);

}

}

1 个答案:

答案 0 :(得分:1)

在控制器Action中,您可以将值传递给Zend_Form对象:

$form->populate($data);

OR     $形式 - > setDefaults($数据);

其中populate()接受一个数组,其中键是表单字段的名称,数组值是字段值。

http://framework.zend.com/manual/1.12/en/zend.form.forms.html#zend.form.forms.elements.values