簡體   English   中英

Zend框架-有效的表單方法不起作用

[英]Zend framework - form method valid not work

我提出了一個問題,我的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: 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(echo“ 1”)。 但是問題發生在第二個if語句上,在瀏覽器上不顯示echo“ 2”。 那么我還有其他方法可以驗證表格嗎?

$ _POST無法識別。 應該替換為$ data = $ this-> _ request-> getPost();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM