Zend Form is not my friend today!
This works :-
Controller:-
public function indexAction()
{
$loginForm = new Application_Form_Login();
//is there a submitted form?
if($this->getRequest()->isPost()){
//yes there is so process it.
$formdata = $this->getRequest()->getPost();
if($loginForm->isValid($formdata)){
$user_logon = $loginForm->getValue('user_name');
$user_pw = $loginForm->getValue('user_pw');
if($this->authenticate($user_logon, $user_pw)){
$this->_redirect();
}
} else {
$this->view->errors = $loginForm->getMessages();
}
}
$this->view->loginForm = $loginForm;
Form
class Application_Form_Login extends Zend_Form
{
public function init()
{
/* Form Elements & Other Definitions Here ... */
$this ->setName('Login');
$this ->setAction('login')
->setMethod('post');
$name = new App_Form_Element_Text('user_name');
$name ->setLabel('User Name')
->setRequired(true);
$pword = new Zend_Form_Element_Password('user_pw');
$pword ->setLabel('Password')
->setRequired(true)
->addValidator('Alnum');
$submit = new Zend_Form_Element_Submit('Submit');
$submit ->setAttrib('id', 'Submit');
$this->addElements(array($name, $pword, $submit));
// buttons do not need labels
$submit->setDecorators(array(
array('ViewHelper'),
array('Description'),
array('HtmlTag', array('tag' => 'span', 'class'=>'submit-group')),
));
}
}
This doesn't!
Controller
public function addAction()
{
$addform = new Application_Form_Student_Add();
//has a form been submitted?
if($this->getRequest()->isPost()){
if(isset($_POST['Cancel'])) $this->_redirect('/student');
$formdata = $thi开发者_如何转开发s->getRequest()->getPost();
if($addform->isValid($formdata)){
Zend_Debug::dump($formdata);
} else {
$this->view->errors = $addform->getMessages();
}
}
$this->view->addForm = $addform->generate();
}
Form
public function init()
{
$this->studentform = new Zend_Form();
$baseUrl = new Zend_View_Helper_BaseUrl();
$action = $baseUrl->baseUrl() . "/student/add";
$this->studentform->setAction($action);
$this->studentform->setName('addStudent');
$this->studentform->setMethod('post');
$student_title = new App_Form_Element_Text('student_title');
$student_title ->setLabel('Titletest')
->setRequired(true);
$cancel = new Zend_Form_Element_Submit('Cancel');
$submit = new Zend_Form_Element_Submit('Submit');
$this->studentform->addElement($student_title);
$this->studentform->addElement($cancel);
$this->studentform->addElement($submit);
}
Both display properly, however the first will validate and the second won't. The first will display error messages when a blank form is submitted, however the 2nd always seems to pass validation regardless of the values entered.
I've been looking at this for hours and it just probably needs somebody else to look at the code and point out the blindingly obvious to me.
In both cases the view just echos the form out.
In the working code you use $this to refer to your form in init in the second version you use $this->studentForm.
So I would be curious to know why the codes differ there and what object is that second init from. The code you posted is different there.
Something tells me in your second controller you should be using
$addform->studentForm->stuff
Since its not an instance of Zend_Form but an object containing a studentForm Zend_Form.
精彩评论