Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [ZendFramework] Rejestracja
Forum PHP.pl > Forum > PHP > Frameworki
uirapuru
  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...
pgrzelka
  1. <?php
  2.  
  3. require_once 'Zend/Validate/Abstract.php';
  4.  
  5. class Zend_Validate_PasswordConfirmation extends Zend_Validate_Abstract
  6. {
  7.    const NOT_MATCH = 'isNotMatch';
  8.  
  9.    protected $_messageTemplates = array(
  10.        self::NOT_MATCH => "Podane hasła nie są identyczne"
  11.    );
  12.  
  13.    public function isValid($value)
  14.    {
  15.        $this->_setValue((string) $value);
  16.  
  17.        if (is_string($value)
  18.            && (($_POST['password'] !== $value))
  19.        ) {
  20.            $this->_error();
  21.            return false;
  22.        } elseif (!is_string($value) && empty($value)) {
  23.            $this->_error();
  24.            return false;
  25.        }
  26.  
  27.        return true;
  28.    }
  29.  
  30. }
  31. ?>
nexis
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. ?>
uirapuru
nexis: zrobiłem w miarę podobnie już wczoraj, validator korzysta z $context i porownuje haslo2 z $context["haslo1"] i wywala odpowiedni blad, takze dzieki wszystkim za pomoc! smile.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.