I have come up with a problem that my zend form valid does not work? Can anyone tell me how can I fix that problem? The code - form setup:
<?php
class Application_Form_Register extends Zend_Form
{
public function init()
{
/* Form Elements & Other Definitions Here ... */
$this->setMethod('POST');
$this->setAttrib('class', 'register');
$this->setAttrib('id', 'register_form');
//Add email field
$this->addElement('text', 'register_email', array(
'label' => 'Your Email Address:',
'required' => true,
'filters' => array('StringTrim'),
'validators' => array('EmailAddress',)
)
);
$email_field = $this->getElement('register_email');
$email_field->setDecorators(array(
'ViewHelper',
'Label',
new Zend_Form_Decorator_HtmlTag(array('tag' => 'div', 'id' => 'register_email_label'))
));
$email_field->setAttrib('class', 'register_field');
$email_field->setAttrib('placeholder', 'eg: example@gmail.com');
//Add retype email
$this->addElement('text', 'retype_register_email', array(
'label' => 'Retype Your Email Address:',
'required' => true,
'filters' => array('StringTrim'),
'validators' => array('EmailAddress',)
)
);
$emailconfim_field = $this->getElement('retype_register_email');
$emailconfim_field->setDecorators(array(
'ViewHelper',
'Label',
new Zend_Form_Decorator_HtmlTag(array('tag' => 'div', 'id' => 'register_email_label'))
));
$emailconfim_field->setAttrib('class', 'register_field');
$emailconfim_field->setAttrib('placeholder', 'This field will be used when you login');
//Add first name field
$this->addElement('text', 'register_first_name', array(
'label' => 'First name: ',
'required' => true,
'filters' => array('StringTrim'),
)
);
$name_field = $this->getElement('register_first_name');
$name_field->setDecorators(array(
'ViewHelper',
'Label',
new Zend_Form_Decorator_HtmlTag(array('tag' => 'div', 'id' => 'register_name_label'))
));
$name_field->setAttrib('class', 'register_field');
$name_field->setAttrib('placeholder', 'eg: Michael');
//Add last name field
$this->addElement('text', 'register_last_name', array(
'label' => 'Last name: ',
'required' => true,
'filters' => array('StringTrim'),
)
);
$name_field = $this->getElement('register_last_name');
$name_field->setDecorators(array(
'ViewHelper',
'Label',
new Zend_Form_Decorator_HtmlTag(array('tag' => 'div', 'id' => 'register_name_label'))
));
$name_field->setAttrib('class', 'register_field');
$name_field->setAttrib('placeholder', 'eg: Lee');
//Add password field
$this->addElement('password', 'register_password', array(
'label' => 'Password: ',
'required' => true,
'filters' => array('StringTrim'),
)
);
$password_field = $this->getElement('register_password');
$password_field->setDecorators(array(
'ViewHelper',
'Label',
new Zend_Form_Decorator_HtmlTag(array('tag' => 'div', 'id' => 'register_password_label'))
));
$password_field->setAttrib('class', 'register_field');
$password_field->setAttrib('placeholder', 'Enter your password here');
//Add retype password field
$this->addElement('password', 'register_retype_password', array(
'label' => 'Confirm your password: ',
'required' => true,
'filters' => array('StringTrim'),
)
);
$password_confim_field = $this->getElement('register_retype_password');
$password_confim_field->setDecorators(array(
'ViewHelper',
'Label',
new Zend_Form_Decorator_HtmlTag(array('tag' => 'div', 'id' => 'register_repassword_label'))
));
$password_confim_field->setAttrib('class', 'register_field');
$password_confim_field->setAttrib('placeholder', 'Retype your password here');
//Add question field
$this->addElement('select', 'password_recovery_question', array(
'label' => 'Choose one question for password recovery',
'value' => 'question_list',
'multiOptions' => array(
'null' => 'Select one question',
'Name of your secondary school?' => 'Name of your secondary school?',
'What is your favorite game?' => 'What is your favorite game?',
'What is your home town?' => 'What is your home town?',
'What is your dream?' => 'What is your dream?',
'Whos is your best friend?' => 'Whos is your best friend?',
)
)
);
$password_question = $this->getElement('password_recovery_question');
$password_question->setDecorators(array(
'ViewHelper',
'Label',
new Zend_Form_Decorator_HtmlTag(array('tag' => 'div', 'id' => 'register_question_label'))
));
$password_question->setAttrib('class', 'register_field');
//Add answer to question field
$this->addElement('text', 'answer_to_question', array(
'label' => 'Your answer: ',
'required' => true,
'filters' => array('StringTrim'),
)
);
$answer = $this->getElement('answer_to_question');
$answer->setDecorators(array(
'ViewHelper',
'Label',
new Zend_Form_Decorator_HtmlTag(array('tag' => 'div', 'id' => 'register_answer_label'))
));
$answer->setAttrib('class', 'register_field');
//Add register button
$this->addElement('submit', 'submit', array(
'ignore' => true,
'label' => '注册',
)
);
$button = $this->getElement('submit');
$button->setDecorators(array(
'ViewHelper',
array('HtmlTag', array('tag' => 'div', 'id' => 'register_submit_label'))
));
$button->setAttrib('class', 'register_button');
}
}
The code - signup code:
public function signupAction()
{
// action body
$users = new Application_Model_DbTable_User();
$form = new Application_Form_Register();
$this->view->form = $form;
if($this->getRequest()->isPost()) {
echo "1";
if($form->isValid($_POST)) {
echo "2";
$data = $form->getValues();
$data_insert = array(
'c_email' => $data['register_email'],
'c_fname' => $data['register_first_name'],
'c_lname' => $data['register_last_name'],
'c_question' => $data['password_recovery_question'],
'c_answer' => $data['answer_to_question'],
'c_password' => sha1($data['register_password']),
);
$users->insert($data_insert);
if($data['register_password'] != $data['register_retype_password']) {
echo $this->view->errorMessage = 'Password does not match!';
return;
} elseif ($users->checkUnquie($data['register_email'])) {
echo $this->view->errorMessage = 'Username already exist';
return;
} else {
$users->insert($data_insert);
}
}
}
}
The problem is when I submit my form with data, it cannot read my data from form field. I have tried to debug, so I add echo after first if statement which does work(echo "1"). But the problem occur on the second if statement, echo "2" does not display on browser. So is there any other method that I valid the form?
$_POST is not recognized. should replace by $data = $this->_request->getPost();