function index($locator) { $model = $this->_load()->model('Users'); $form = new A_Model_Form(); $form->addField($model->getFields()); # $input->setRequired($model->getRequired()); // get required fields from model # $input->addRule(new AddressRules); // modularity! # $input->addRule(new PhoneRules); // modularity! $form->addRule($model->getRules()); // modularity! // 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->addRule(new MyRules); // $form->addFilter(new MyFilters);dump($form); $view = $this->_load()->view('Form4'); if ($this->request->isPost()) { if ($form->isValid($this->request)) { try { $model->save($form); // redirect to user detail page or whatever } catch (A_Model_Exception $e) { // bummer! } exit; } else { $view->setErrorMsg($form->getErrorMsg()); } $view->setValues($form->getValues()); } echo $view->render(); }
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); }
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()); }
public function setpassword($locator) { $request = $this->request; $usermodel = $this->_load('app')->model('users'); $form = new A_Model_Form(); $field = new A_Model_Form_Field('password'); $field->addRule(new A_Rule_Notnull('password', 'Please fill in the password.')); $field2 = new A_Model_Form_Field('passwordagain'); $field2->addRule(new A_Rule_Notnull('passwordagain', 'Please fill in the passwordagain.')); $form->addRule(new A_Rule_Match('passwordagain', 'password', 'Fields password and passwordagain do not match')); $form->addField($field); $form->addField($field2); if ($request->isPost()) { $resetkey = $request->get('resetkey'); if ($form->isValid($this->request)) { $resetkey = $request->get('resetkey'); $result = $usermodel->findResetkey($resetkey); if ($result === true) { $usermodel->resetPassword($request->get('password'), $resetkey); $this->response->setPartial('maincontent', 'user/password/resetpasswordsuccess'); } else { $this->response->setPartial('maincontent', 'user/password/resetpassworderror', array('message' => 'reset failed')); } } else { $this->response->setPartial('maincontent', 'user/password/resetpasswordform', array('message' => 'reset form invalid', 'resetkey' => $resetkey)); } } }