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()); }
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)); }