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) { $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); } }
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); }