Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [ZendFramework] Rejestracja
uirapuru
post 17.07.2009, 20:58:53
Post #1





Grupa: Zarejestrowani
Postów: 182
Pomógł: 9
Dołączył: 30.04.2005

Ostrzeżenie: (0%)
-----


  1. <?php
  2. $this->view->title = "Rejestracja";
  3.  
  4.        $register_form = new RegisterForm();
  5.        $register_form->setAction($this->baseUrl.'/user/rejestracja/');
  6.        $this->view->formResponse = '';
  7.        $this->view->register_form = $register_form;
  8.  
  9.        if ($this->getRequest()->isPost()) {
  10.            $auth = Zend_Auth::getInstance();
  11.            $auth->clearIdentity();
  12.  
  13.            if ($register_form->isValid($_POST)) {
  14.                
  15.                $activation = md5(time());
  16.                $seed = time();
  17.                $haslo = md5($seed.$this->_request->password);
  18.                
  19.                $data = array(
  20.                       "username" => $this->_request->username,
  21.                       "md5_pass" => $haslo,
  22.                       "seed" => $seed,
  23.                    "email" => $this->_request->email,
  24.                    "rank" => 0,
  25.                    "activation" => $activation,    
  26.                );
  27.                $user = new user();
  28.                $user->insert($data);
  29.                $this->view->activation = $activation;
  30.  
  31.            } else {
  32.                $this->view->formResponse = 'Wystapil blad!';
  33.                $register_form->populate($_POST);
  34.            }
  35.        }
  36. ?>


czy do insert'a w takiej formie mogę brać wprost z $this->_request, czy lepiej wyciągać dane jakoś z formularza?

(ps. moje pierwsze kroki, jak ktoś ma sugestie, to bardzo proszę o podzielenie się nimi ze mną smile.gif )

No dobra, widzę, że nie pomożecie smile.gif ale mam inny problem.

z Zend_Form zrobiłem sobie formularz do rejestracji. Jak skonstruować validator taki, żeby sprawdzał czy podane hasło1 i hasło2 sa identyczne? Nie chodzi mi o kod, a raczej o filozofie zadziałania tego. Dodać do gadżetu hasło2 validator, który odczyta skądś wartość hasło1 i wywali błąd w razie niezgodności? a jeśli tak, to gdzie to hasło1 mogę przechować bezpiecznie, tak, żeby było dostępne z validatora dla hasło2? bo w moim mniemaniu zend_registry to ostateczność w tym momencie...
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
nexis
post 19.07.2009, 17:58:28
Post #2





Grupa: Zarejestrowani
Postów: 1 012
Pomógł: 109
Dołączył: 26.09.2003
Skąd: nexis.pl

Ostrzeżenie: (0%)
-----


Ja proponuje w ten sposób:

controllers/ProfileController.php
  1. <?php
  2. class ProfileController extends Zend_Controller_Action
  3. {
  4.    protected $_model;
  5.  
  6.    /**
  7.      * @return Model_User
  8.      */
  9.    protected function _getModel()
  10.    {
  11.        if (null === $this->_model)
  12.        {
  13.            require_once APPLICATION_PATH.'/models/User.php';
  14.            $this->_model = new Model_User();
  15.        }
  16.        return $this->_model;
  17.    }
  18.  
  19.    /**
  20.      * Get registration form form
  21.      *
  22.      * @return Form_Profile_Registration
  23.      */
  24.    protected function _getRegistrationForm()
  25.    {
  26.        require_once APPLICATION_PATH.'/forms/Profile/Registration.php';
  27.        $form = new Form_Profile_Registration();
  28.        $identical = $form->getElement('password2')->getValidator('Identical');
  29.        $identical->setToken($this->_request->getPost('password'));
  30.        return $form;
  31.    }
  32.  
  33.    /**
  34.      * User registration
  35.      *
  36.      * @return void
  37.      */
  38.    public function registrationAction()
  39.    {
  40.        $request = $this->getRequest();
  41.        $form    = $this->_getRegistrationForm();
  42.        if ($this->getRequest()->isPost())
  43.        {
  44.            if ($form->isValid($request->getPost()))
  45.            {
  46.                $model = $this->_getModel();
  47.                $model->add($form->getValues());
  48.                return $this->_helper->redirector('index', 'index');
  49.            }
  50.        }
  51.  
  52.        $this->view->form = $form;
  53.    }
  54. }
  55. ?>


form/Profile/Registration.php
  1. <?php
  2. class Form_Profile_Registration extends Zend_Form
  3. {
  4.    public function init()
  5.    {        
  6.        $this->setMethod('post');
  7.        
  8.        $this->addElement('text', 'email', array(
  9.            'label'      => 'Adres e-mail:',
  10.            'required'   => true,
  11.            'filters'    => array(
  12.                'StringTrim',
  13.                'StringToLower'
  14.            ),
  15.            'validators' => array(
  16.                'EmailAddress'
  17.            )
  18.        ));
  19.        
  20.        $this->addElement('password', 'password', array(
  21.            'label'      => 'Hasło:',
  22.            'required'   => true,
  23.            'validators' => array(
  24.                array('StringLength', false, array(6))
  25.            )
  26.        ));
  27.        
  28.        $this->addElement('password', 'password2', array(
  29.            'label'      => 'Powtórz hasło:',
  30.            'required'   => true,
  31.            'ignore'     => true,
  32.            'validators' => array(
  33.                'Identical'
  34.            )
  35.        ));
  36.        
  37.        $this->addElement('submit', 'submit', array(
  38.            'label'    => $translate->translate('NEXT'),
  39.            'ignore'   => true
  40.        ));
  41.    }
  42. }
  43. ?>


--------------------
Zend Certified Engineer

Kliknij POMÓGŁ jeśli moja odpowiedź okazała się użyteczna!
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 26.06.2025 - 21:54