我遇到了一个问题,我的zend form valid 不起作用?谁能告诉我如何解决这个问题?代码 - 表单设置:
<?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');
}
}
代码 - 注册代码:
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);
}
}
}
}
问题是当我提交带有数据的表单时,它无法从表单字段中读取我的数据。我试图调试,所以我在第一个 if 语句之后添加了 echo(回声“1”)。但是问题出现在第二个 if 语句上,echo "2" 不会显示在浏览器上。那么还有其他方法可以验证表格吗?