使用setAction时,Zend表单验证失败
I would like to validate form and if validation passes it has to be submitted to connectTracking Action.
At present form gets submitted to connectTracking action before it passes validation.
Controller:
public function easyAction(){
$auth = Zend_Auth::getInstance();
$user_id = $this->_helper->Utilities->getCurrentUserId();
if ($auth->hasIdentity()) {
$form = new Application_Form_easy();
$form->submit->setLabel('Submit');
$this->view->form = $form;
$formData = $this->getRequest()->getPost();
if ( ($this->getRequest()->getPost('easyform', false)) && ($form->isValid($this->getRequest()->getPost())) ) {
$username = $form->getValue('username');
$password = $form->getValue('password');
$releasedata = array('username' => $username, 'password' => $password);
}
}
}
/application/forms/easy.php
class Application_Form_easy extends Zend_Form {
public function init() {
$this->setMethod('post');
$this->setName('easyform');
$this->setAction('/index.php/releases/connectTracking'); # Submitting to different action
$username = new Zend_Form_Element_Text('username');
$username->setLabel('Username * :')
->setRequired(true)
->addValidator('NotEmpty', true, array('messages' => 'Please enter Username'));
$password = new Zend_Form_Element_Password('password');
$password->setLabel('Password * :')
->setRequired(true)
->addValidator('NotEmpty', true, array('messages' => 'Please enter password '));
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'easyform');
$submit->setAttrib('name', 'easyform');
$this->addElements(array($username, $password, $submit));
}
}
Could you please let me know how shall I validate the form and then submit to action
Many Thanks!
Basically isValid
is the method to validate a form:
$form = new Application_Form_easy();
$formData = $this->_request->getPost();
if($form->isValid($formData)){
//Form is valid, do your stuff
} else {
//Form is not valid re-populate data
$form->populate($formData);
}
In your case you are setting the form action this one /index.php/releases/connectTracking
and you have written your validation code in easyAction
which is wrong.
you have two ways here to achieve your requirement;
1) Set action of your form this /controllerName/easy
and validate your form in easyAction
(which you are doing right now) and if form is valid then redirect control to releases/connectTracking
action.
2) Another way is to write your validation code in releases/connectTracking
Action and if form is not valid then redirect control again to easy
Action with all the POST
data to re-populate the form. And if form is valid then continue your stuff at releases/connectTracking
action.
E.g. for first solution:
Controller:
public function easyAction(){
$auth = Zend_Auth::getInstance();
$user_id = $this->_helper->Utilities->getCurrentUserId();
if ($auth->hasIdentity()) {
$form = new Application_Form_easy();
$form->submit->setLabel('Submit');
$this->view->form = $form;
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$username = $form->getValue('username');
$password = $form->getValue('password');
$releasedata = array('username' => $username, 'password' => $password);
//Form is valid, hence forward controller to another action with formData
$this->_request->setPost(array('formData' => $formData));
//Write actual module name in the below line
$this->_forward('connectTracking', 'releases', 'moduleName');
//$this->_redirect('/releases/connectTracking');
} else {
$form->populate($formData);
}
}
}
Form:
class Application_Form_easy extends Zend_Form {
public function init() {
$this->setMethod('post');
$this->setName('easyform');
//Write actual name of your controller here
$this->setAction('/controllerName/easy'); # Submitting to same action
$username = new Zend_Form_Element_Text('username');
$username->setLabel('Username * :')
->setRequired(true)
->addValidator('NotEmpty', true, array('messages' => 'Please enter Username'));
$password = new Zend_Form_Element_Password('password');
$password->setLabel('Password * :')
->setRequired(true)
->addValidator('NotEmpty', true, array('messages' => 'Please enter password '));
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'easyform');
$submit->setAttrib('name', 'easyform');
$this->addElements(array($username, $password, $submit));
}
}