Why doesn't Zend_Form :: getValues ββ() return expected values ββoutside of MVC?
I am using Zend_Form (complete library on include_path) but not using MVC . $_POST
values ββare expected, but $form->getValues()
returns null for the key containing the correct strings in $ _POST. I expected to $form->getValues()
return the correct string for the "fullname" key. Here is the form:
class MyForm extends Zend_Form {
public function init() {
$this->setName('myform')
->setAction($_SERVER['PHP_SELF'])
->setMethod('post');
$fullname = $this->createElement('text', 'fullname')
->setLabel('What is your name?');
$this->addElement($fullname);
$this->addElement('submit', 'submit');
}
}
Here is the HTML generated for the form:
<form id="myform" name="myform" enctype="application/x-www-form-urlencoded" action="/classes_test/index.php" method="post">
<dl class="zend_form">
<dt id="fullname-label">
<label for="fullname" class="optional">What is your name?</label>
</dt>
<dd id="fullname-element">
<input type="text" name="fullname" id="fullname" value="">
</dd>
<dt id="submit-label"> 
</dt>
<dd id="submit-element">
<input type="submit" name="submit" id="submit" value="submit">
</dd>
</dl>
</form>
Here's the processing:
$request = new Zend_Controller_Request_Http();
$form = new MyForm;
$form->setView(new Zend_View);
if ($request->isPost()) {
var_dump($_POST);
$data = $form->getValues();
var_dump($data);
if ($form->isValid($request->getPost()) {
...
}
}
Here is var_dump $ _POST:
array
'fullname' => string 'My Name' (length=7)
'submit' => string 'submit' (length=6)
Here is var_dump from $ data:
array
'fullname' => null
Why is 'fullname' null?
source to share
Zend_Form
does not have direct access to data from $_POST
, you need to transfer data. One way to do this is through a challenge isValid()
. So the answer to your question is - fullname is null because there is no data in the form object yet.
If you try this instead:
if ($request->isPost()) {
var_dump($_POST);
if ($form->isValid($request->getPost()) {
$data = $form->getValues();
var_dump($data);
}
}
you will get the expected result.
source to share