0
我想出了一個問題,我的zend表單有效不起作用?任何人都可以告訴我如何解決這個問題? 代碼 - 形式設置:Zend框架 - 表單方法有效不起作用
<?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: [email protected]');
//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(echo「1」)。 但是,如果在瀏覽器上沒有顯示echo「2」語句,第二個問題就會發生。那麼有沒有其他方法可以驗證表單?
你是如何設置你的表單的?你是否試圖確定$ _POST中的值是否與它們應該根據表單中的元素一致? '$ form-> isValid()'完全按照它應該工作的方式工作。 –
我已添加我的註冊表單代碼。你能幫我檢查一下嗎? –
如何在你的'register.php'中有6個字段,但你的'Application_Form_Register'模型中只有一個(register_email)? 如果你想調試發生了什麼,在if($ form-> isValid($ _ POST))之後添加'else'塊,我認爲你的數據是無效的。 – Technoh