function testModel_FormSubmitted() { $request = new Model_FormTest_Request(); $model = new A_Model_Form(); // check not post or $this->assertFalse($model->isSubmitted($request)); // test submit param mode, post/get not required $request->set('submit', 'foo'); $model->setMethod(''); $model->setSubmitParameterName('submit'); $this->assertTrue($model->isSubmitted($request)); // test post mode $request->setMethod('POST'); // post but no submit param name $model->setSubmitParameterName(''); $this->assertTrue($model->isSubmitted($request)); // post and submit param name $model->setSubmitParameterName('submit'); $this->assertTrue($model->isSubmitted($request)); }
function index($locator) { $model = new A_Model_Form(); // get fieldeter object from controller $model->addField($field1 = new A_Model_Form_Field('field1')); $field1->addFilter(new A_Filter_Regexp('/[^0-9]/', '')); $field1->addRule(new A_Rule_Notnull('field1', 'Please enter Field 1')); $field1->addRule(new A_Rule_Range(1, 10, 'field1', 'Field 1 must be 1-10')); $field1->setType(array('renderer' => 'A_Html_Form_Select', 'values' => array(5, 10, 15), 'labels' => array('five', 'ten', 'fifteen'))); $model->addField($field2 = new A_Model_Form_Field('field2')); $field2->addFilter(new A_Filter_Regexp('/[^0-9]/', '')); $field2->addRule(new A_Rule_Notnull('field2', 'Please enter Field 2')); $field2->addRule(new A_Rule_Match('field1', 'field2', 'Field 2 must match Field 1')); $field2->setType(array('renderer' => 'A_Html_Form_Text', 'size' => '10')); $model->addField($field3 = new A_Model_Form_Field('field3')); $field3->addFilter(new A_Filter_Regexp('/[^a-zA-Z]/', '')); $field3->addRule(new A_Rule_Length(5, 20, 'field3', 'Field 3 must be 5-20 characters')); // create fieldeter object then add it to the controller $model->addField($field4 = new A_Model_Form_Field('field4')); $field4->addFilter(new A_Filter_Regexp('/[^a-zA-Z]/', '')); $field4->addFilter(new A_Filter_ToUpper()); $field4->addRule(new A_Rule_Notnull('field4', 'Please enter Field 4')); $model->excludeRules('field3'); //$model->run($locator); $request = $locator->get('Request'); if ($model->isValid($request)) { echo 'DONE<br/><br/><a href="../">Return to Examples</a>'; } else { if (!$model->isSubmitted()) { $model->set('field1', 15); $model->set('field2', 'init'); $model->set('field3', 'init'); $model->set('field4', 'init'); } //dump($model); // create HTML form generator $form = new A_Html_Form(); $form->setModel($model)->setWrapper('A_Html_Div', array('class' => 'fieldclass', 'style' => 'border:1px solid red;'))->select(array('name' => 'field1', 'values' => array(5, 10, 15), 'labels' => array('five', 'ten', 'fifteen'), 'id' => 'field1', 'label' => 'Field 1 (Numbers only in range 1-10)', 'after' => $field1->getErrorMsg(', ')))->text(array('name' => 'field2', 'id' => 'field2', 'label' => 'Field 2 (Must match Field 1)', 'after' => $field2->getErrorMsg(', ')))->text(array('name' => 'field3', 'id' => 'field3', 'label' => 'Field 3 (Letters only min length 5)', 'after' => $field3->getErrorMsg(', ')))->text(array('name' => 'field4', 'id' => 'field4', 'label' => 'Field 4 (Convert letters to uppercase)', 'after' => $field4->getErrorMsg(', ')))->submit('submit', 'Submit'); echo $form->render(); # include 'templates/example_form2.php'; } }
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); }
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'); } }