I have a Zend form and a URL to this form.
The URL has 3 parameters firstname, lastname and email.
In my form i have 3 input fields that also has firstname, lastname and email.
Whenever I submit the form. It’s getting the values from the URL parameters instead of the form input fields.
Is there a way to get values in the form?
I know that the parameters from url has same name as the form input fields*
URL: http://mywebsite.com/thank-you?key=e72671a8640b45c2401afe887b9b530a&first_name=First&last_name=User&email=user_3fdfsdfs@gmail.com
Zend action controller of the form:
$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 form:
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);
}
}
In your controller Action you can pass values to Zend_Form object:
OR
$form->setDefaults($data);
Where populate() takes an array where the keys are the names of the form fields and array values are the field values.
http://framework.zend.com/manual/1.12/en/zend.form.forms.html#zend.form.forms.elements.values