Exemple #1
0
 function testModel_FormValid()
 {
     $request = new Model_FormTest_Request();
     $model = new A_Model_Form();
     // check not post
     $this->assertFalse($model->isValid($request));
     // test post mode no fields or rules
     $request->setMethod('POST');
     $this->assertTrue($model->isValid($request));
     // test post mode with field and param value but no rules
     $request->set('foo', 'bar');
     $field = $model->newField('foo');
     $this->assertTrue($model->isValid($request));
     $this->assertEqual($model->get('foo'), 'bar');
     // add rule
     $field->addRule(new A_Rule_Notnull('foo', 'error'));
     // param not set
     $request->set('foo', null);
     $this->assertFalse($model->isValid($request));
     $this->assertTrue($model->isError());
     $this->assertEqual($model->getErrorMsg(), array('foo' => array('error')));
     $this->assertEqual($model->getErrorMsg(' '), 'error');
     // param not set
     $request->set('foo', 'bar');
     $this->assertTrue($model->isValid($request));
 }
 function index($locator)
 {
     $usersmodel = $this->_load()->model('Users');
     $view = $this->_load()->view('Form');
     // Instantiate a new form model/controller
     $form = new A_Model_Form();
     // Hand the Form the fields and rules from the model
     //	$form->addRule($usersmodel->getRules());
     $form->addField($usersmodel->getFields());
     // Now add an additional field, the second password field. Which must match the first password field.
     // The $form get the Rules for the first password field from $usersmodel
     $form->addField($passwordfield = new A_Model_Form_Field('password2'));
     // now we add an additional rule, specific for the form we are dealing with.
     $form->addRule(new A_Rule_Match('password', 'password2', 'Password 2 must match Password 1'));
     //$form->run($locator);
     //dump($form);
     // ask the form if it is valid. The form checks internally if the model fields are valid?
     if ($form->isValid($this->request)) {
         // save
         $usersmodel->save($form->getSaveValues());
         // redirect to user detail page or whatever
     } else {
         // show errors if submitted
         $view->setErrorMsg($form->getErrorMsg());
     }
     $view->setValues($form->getValues());
     $this->response->setRenderer($view);
 }
Exemple #3
0
 function edit($locator)
 {
     $template = $this->_load('controller')->template();
     $posts = $this->_load('app')->model('posts', $locator->get('Db'));
     $form = new A_Model_Form();
     // Hand the Form the fields and rules from the model
     $form->addRule($posts->getRules());
     $form->addField($posts->getFields());
     // Get list of users to create authorlist in form
     $users = $this->_load('app')->model('users', $locator->get('Db'));
     $authorlist = $users->findAuthorlist()->toArray();
     $form->newField('authorlist');
     $form->set('authorlist', $authorlist);
     // ask the form if it is valid. The form checks internally if the model fields are valid
     if ($form->isValid($this->request)) {
         // save
         $result = $posts->save($form->getSaveValues());
         if ($result->isError()) {
             $form->setErrorMsg('databaseerror', 'Could not save to the database');
         }
     } elseif (!$this->_request()->has('save') && $this->_request()->has('id')) {
         $id = $this->_request()->get('id');
         // load data
         $rows = $posts->find($id);
         if (isset($rows[0])) {
             foreach ($rows[0] as $name => $value) {
                 $form->newField($name)->setValue($value);
             }
         }
     }
     $template->set('errorMsg', $form->getErrorMsg(', '));
     $template->import($form->getValues());
     $this->response->set('maincontent', $template->render());
 }
Exemple #4
0
 function index($locator)
 {
     $usersession = $locator->get('UserSession');
     $usersmodel = $this->_load()->model('Users');
     $form = new A_Model_Form();
     $form->addField($usersmodel->getFields());
     $base = $locator->get('Config')->get('BASE');
     // If the user is loged in:
     if ($usersession->isLoggedIn()) {
         // and wants to log out
         if ($this->request->get('op') == 'logout') {
             $usersession->logout();
             $this->_flash('Message', 'You are now logged-out');
             $this->response->setRedirect($base);
             // For now I do a redirect but you can also do:
             //$layout = $this->_load()->template('login.tpl');
             //$layout->set('message', 'you are now loged out');
         } else {
             // else just show the logout form
             $layout = $this->_load()->template('logout');
             $layout->set('BASE', $base);
             $layout->set('message', 'Please log out');
             $this->response->setRenderer($layout);
         }
     } else {
         // If not loged in and user wants to log in
         $layout = $this->_load()->template('login');
         if ($this->_request('op') == 'login') {
             if ($form->isValid($this->request)) {
                 if ($row = $usersmodel->findAuthorized($form->get('username'), $form->get('password'))) {
                     $usersession->login($form->get('username'));
                     $this->_flash('Message', 'You are now logged-in');
                     $url = 'http://skeleton/examples/login/';
                     $this->response->setRedirect($url);
                 }
             }
         } else {
             $layout->set('message', 'Please log in');
         }
         $layout->set('errmsg', 'Please fill in correct username and password');
         $layout->set('errmsg', $form->getErrorMsg(' ,'));
         $layout->set('BASE', $base);
         $layout->set('username', $form->get('username'));
         $this->_response()->setRenderer($layout);
     }
 }
Exemple #5
0
 public function index($locator)
 {
     $request = $locator->get('Request');
     $response = $locator->get('Response');
     $usersession = $locator->get('UserSession');
     $errmsg = '';
     $usernamestr = '';
     if (!$usersession->isLoggedIn()) {
         if ($request->get('op') == 'login') {
             $form = new A_Model_Form();
             $username = new A_Model_Form_Field('username');
             $username->addFilter(new A_Filter_Regexp('/[^a-zA-Z0-9]/', ''));
             $username->addFilter(new A_Filter_ToLower());
             $username->addRule(new A_Rule_Notnull('username', 'Username required'));
             $username->addRule(new A_Rule_Length(4, 20, 'username', 'Username must be 4 characters long'));
             $form->addField($username);
             $password = new A_Model_Form_Field('password');
             $password->addFilter(new A_Filter_Regexp('/[^a-zA-Z0-9\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\-\\_\\=\\+]/', ''));
             $password->addRule(new A_Rule_Notnull('password', 'Password required'));
             $password->addRule(new A_Rule_Length(4, 20, 'password', 'Password must be 4 characters long'));
             $form->addField($password);
             if ($form->isValid($request)) {
                 $user = $this->_load()->model('UsersModel');
                 if ($row = $user->findAuthorized($username->value, $password->value)) {
                     $usersession->merge($row);
                     $usersession->login($username->value, $password->value);
                 }
             } else {
                 $errmsg = 'Errors: ' . $form->getErrorMsg(', ');
                 $usernamestr = $username->value;
             }
         }
     }
     if ($usersession->isLoggedIn()) {
         $page_template = new A_Template_Strreplace('templates/logout.html');
     } else {
         $page_template = new A_Template_Strreplace('templates/login.html');
         $page_template->set('errmsg', $errmsg);
         $page_template->set('username', $usernamestr);
     }
     $response->setContent($page_template->render());
 }
Exemple #6
0
 function index($locator)
 {
     $session = $locator->get('Session');
     $user = $locator->get('UserSession');
     #dump($user, 'USER: '******'';
     $session->start();
     // controller and view use session
     $form = new A_Model_Form();
     $field = new A_Model_Form_Field('userid');
     $field->addRule(new A_Rule_Notnull('userid', 'User ID required'));
     $form->addField($field);
     $field = new A_Model_Form_Field('password');
     $field->addRule(new A_Rule_Notnull('password', 'Password required'));
     $form->addField($field);
     // If username and password valid and isPost
     if ($form->isValid($this->request)) {
         // How to translate URL in correct action variable?
         $model = $this->_load('app')->model('users');
         $userdata = $model->login($form->get('userid'), $form->get('password'));
         if ($userdata) {
             // user record matching userid and password found
             unset($userdata['password']);
             // don't save passwords in the session
             $user->login($userdata);
             $this->_redirect($locator->get('Config')->get('BASE') . 'login/');
             // build redirect URL back to this page
         } else {
             $errmsg = $model->getErrorMsg();
         }
     } elseif ($form->isSubmitted()) {
         // submitted form has errors
         $errmsg = $form->getErrorMsg(', ');
     }
     $template = $this->_load()->template();
     $template->set('errmsg', $errmsg);
     $template->set('userid', $form->get('userid'));
     $template->set('user', $user);
     $this->response->set('maincontent', $template);
 }
Exemple #7
0
 public function password($locator)
 {
     $session = $locator->get('Session');
     $user = $locator->get('UserSession');
     $session->start();
     $request = $this->request;
     $errmsg = '';
     $form = new A_Model_Form();
     $field = new A_Model_Form_Field('email');
     //$field->addRule(new A_Rule_Notnull('email', 'Please fill in your email address.'));
     $field->addRule(new A_Rule_Email('email', 'Please provide a valid email address.'));
     $form->addField($field);
     // @todo: should we check in db if filled in username even exists
     //$model = $this->_load('app')->model('users');
     $usermodel = $this->_load('app')->model('users');
     if ($request->isPost()) {
         // If password forgot form is posted and is valid
         if ($form->isValid($this->request)) {
             // Retrieve email from user model and send email with istructions
             $userdata = $usermodel->findByEmail($request->get('email'));
             if (empty($userdata)) {
                 // no userdata exists with this email address
                 $this->mailResetPasswordAttemptMessage($request->get('email'));
             } else {
                 // userdata for this email adress exist
                 // Create unique reset key
                 $resetkey = md5(uniqid(rand(), true));
                 // insert reset key in db
                 $usermodel->insertResetkey($resetkey, $userdata['id']);
                 // send email with link and reset key
                 $resetlink = $locator->get('Config')->get('BASE') . 'user/resetpassword?id=' . $resetkey;
                 $this->mailResetPasswordMessage($request->get('email'), $userdata['username'], $resetlink);
             }
             // Show page with instructions
             $this->response->setPartial('maincontent', 'user/password/password-instructions-send');
         } elseif ($form->isSubmitted()) {
             // submitted form has errors
             $errmsg = $form->getErrorMsg(' ');
             $this->response->setPartial('maincontent', 'user/password/password', array('errmsg' => $errmsg));
         }
     } else {
         // Show password forgot page and form
         $this->response->setPartial('maincontent', 'user/password/password');
     }
 }