doudong3570 2014-04-08 05:13
浏览 60
已采纳

在zend框架2中创建登录表单

I am trying to create login page in zend framework2. I have created its view,controller,model and form. Even i m not getting any error.

following my code :

My model as Login.php is :

class Login implements InputFilterAwareInterface
 {
     public $id;
     public $username; 
     public $password;
      protected $inputFilter;

     public function exchangeArray($data)
     {
         $this->id     = (!empty($data['id'])) ? $data['id'] : null;
         $this->username = (!empty($data['username'])) ? $data['username'] : null;
         $this->password  = (!empty($data['password'])) ? $data['password'] : null;
     }
      public function getArrayCopy()
     {
         return get_object_vars($this);
     }

      public function setInputFilter(InputFilterInterface $inputFilter)
     {
         throw new \Exception("Not used");
     }

     public function getInputFilter()
     {
         if (!$this->inputFilter) {
             $inputFilter = new InputFilter();

             $inputFilter->add(array(
                 'name'     => 'id',
                 'required' => true,
                 'filters'  => array(
                     array('name' => 'Int'),
                 ),
             ));

             $inputFilter->add(array(
                 'name'     => 'username',
                 'required' => true,
                 'filters'  => array(
                     array('name' => 'StripTags'),
                     array('name' => 'StringTrim'),
                 ),
                 'validators' => array(
                     array(
                         'name'    => 'StringLength',
                         'options' => array(
                             'encoding' => 'UTF-8',
                             'min'      => 1,
                             'max'      => 100,
                         ),
                     ),
                 ),
             ));

             $inputFilter->add(array(
                 'name'     => 'password',
                 'required' => true,
                 'filters'  => array(
                     array('name' => 'StripTags'),
                     array('name' => 'StringTrim'),
                 ),
                 'validators' => array(
                     array(
                         'name'    => 'StringLength',
                         'options' => array(
                             'encoding' => 'UTF-8',
                             'min'      => 1,
                             'max'      => 100,
                         ),
                     ),
                 ),
             ));

             $this->inputFilter = $inputFilter;
         }

         return $this->inputFilter;
     }
 }

My LoginForm is :

namespace Application\Form;

 use Zend\Form\Form;

 class LoginForm extends Form
 {
     public function __construct($name = null)
     {
         // we want to ignore the name passed
         parent::__construct('tbluser');

         $this->add(array(
             'name' => 'id',
             'type' => 'Hidden',
         ));
         $this->add(array(
             'name' => 'username',
             'type' => 'Text',
             'options' => array(
                 'label' => '',

             ),
             'attributes' => array(
                 'id' => 'username',
                 'class'=>'',
                 'autocomplete'=>'OFF',
                 'max'=>'100',
             ),
         ));
         $this->add(array(
             'name' => 'password',
             'type' => 'Text',
             'options' => array(
                 'label' => '',
             ),
             'attributes' => array(
                 'id' => 'password',
                 'class'=>'',
                 'autocomplete'=>'OFF',
                 'max'=>'100',
             ),
         ));
         $this->add(array(
             'name' => 'submit',
             'type' => 'Submit',
             'attributes' => array(
                 'value' => 'Login',
                 'id' => 'login',
                 'class'=>'btn btn-success',
             ),
         ));


     }
 }

and my controller action code is :

public function indexAction() 
    {

    $form = new LoginForm();
    $request = $this->getRequest();
    $user=new LoginTable();
           if ($request->isPost()) {
             $login = new Login();
             $form->setInputFilter($login->getInputFilter());
             $form->setData($request->getPost());
              if ($form->isValid()) {
                 $data=$login->exchangeArray($form->getData());
                 $user->getUser($this->$username,$this->$password);
                  if($user){    
                    return $this->redirect()->toRoute('album', array(
                 'action' => 'album'));
                    }else{
                    return array('form' => $form);
                    }              
             }

         }

         return array('form' => $form);




    }

my form is not validating at this statement "if ($form->isValid()){}" the exicution is not entering in this statement. I searched out every thing but not able to find solution what i m missing. Can any body help me to get out of this.

  • 写回答

1条回答 默认 最新

  • dongpingwu8378 2014-04-08 08:11
    关注

    Looking at the form it looks as if you have an input filter attached to the hidden field id. If you are looking to register the user you obviously will not have their id before authentication.

    You can test this by adding a else statement of your form and checking the forms messages (because the element is hidden you will not see it output otherwise)

    if ($form->isValid()) {
       // Is valid code here
    } else {
       print_r($form->getMessages());
    }
    

    This will probably output and array with the message "id - this element cannot be empty" or something similar.

    The solution would be to remove the id input filter from the form (and probably the id form element as I can't see how this would be needed).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题