2013-08-28 3 views
0

올바른 제 젠드 양식이 작동하지 않는 문제가 발생 했습니까? 아무도 그 문제를 어떻게 해결할 수 있는지 말해 줄 수 있습니까? 코드 - 양식 설정 :Zend framework - form method valid not work

<?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 문 다음에 에코를 추가합니다 (에코 "1"). 그러나 문제는 두 번째 if 문에서 발생합니다. echo "2"는 브라우저에 표시되지 않습니다. 그래서 양식을 유효하게하는 다른 방법이 있습니까?

+1

양식을 어떻게 설정하셨습니까? $ _POST의 값이 형식의 요소에 따라야하는지 여부를 파악하려고 시도 했습니까? '$ form-> isValid()'는 제대로 동작해야한다. –

+0

등록 양식 코드를 추가했습니다. 그걸 확인하도록 도와 줄 수 있니? –

+0

어떻게'register.php'에 6 개의 필드가 있지만'Application_Form_Register' 모델에는 하나 (register_email) 만 있습니까? 무슨 일이 일어나고 있는지 디버깅하고 싶다면'if ($ form-> isValid ($ _ POST))'다음에'else' 블록을 추가하십시오. 데이터가 단순히 유효하지 않다고 생각합니다. – Technoh

답변

0

$ _POST가 인식되지 않습니다. $ data = $ this -> _ request-> getPost();로 대체해야합니다.