Esempio n. 1
0
 private function _assertPostedValue($postValue, $savedValue, $language, $frontendValue, $allowDecimals = null)
 {
     $m1 = new Kwf_Form_NumberField_TestModel();
     $form = new Kwf_Form();
     $form->setModel($m1);
     $form->setId(1);
     $nrField = $form->add(new Kwf_Form_Field_NumberField('nr', 'Number'));
     $nrField->setAllowDecimals($allowDecimals);
     $form->trlStaticExecute($language);
     $post = array($nrField->getFieldName() => $postValue);
     if ($frontendValue !== false) {
         $post[$nrField->getFieldName() . '-format'] = 'fe';
     }
     $post = $form->processInput(null, $post);
     if (!$form->validate(null, $post)) {
         $form->prepareSave(null, $post);
         $form->save(null, $post);
     }
     $testRow = $m1->getRow(1);
     $this->assertEquals($savedValue, $testRow->nr);
     $values = $form->load(null, $post);
     $this->assertEquals($values['nr'], $postValue);
     if ($frontendValue !== false) {
         $html = $nrField->getTemplateVars($values);
         $this->assertTrue(!!preg_match('#name="nr" value="(.*?)"#', $html['html'], $m));
         $this->assertEquals($frontendValue, $m[1]);
     }
 }
 public function testCreateOnFind()
 {
     $form = new Kwf_Form('form1');
     $form->setModel(new Kwf_Model_FnF(array('data' => array(array('id' => 10, 'test1' => 'foo')))));
     $form->add(new Kwf_Form_Field_TextField('test1'));
     $form->add(new Kwf_Form('form2'))->setModel(new Kwf_Model_FnF())->setCreateMissingRow(true)->setIdTemplate('{0}')->add(new Kwf_Form_Field_TextField('test2'));
     $form->setId(10);
     $data = $form->load(null);
     $this->assertEquals($data, array('form1_test1' => 'foo', 'form1_form2_test2' => ''));
     $data = array('form1_test1' => 'foox', 'form1_form2_test2' => 'barx');
     $form->prepareSave(null, $data);
     $form->save(null, $data);
     $this->assertEquals($form->getRow(null)->test1, 'foox');
     $this->assertEquals($form->fields['form2']->getRow($form->getRow(null))->test2, 'barx');
 }
Esempio n. 3
0
 public function preDispatch()
 {
     parent::preDispatch();
     $t = microtime(true);
     if (!isset($this->_form)) {
         if (isset($this->_formName)) {
             $this->_form = new $this->_formName();
         } else {
             $this->_form = new Kwf_Form();
         }
     }
     foreach ($this->_fields as $k => $field) {
         if (!isset($field['type'])) {
             throw new Kwf_Exception("no type for field no {$k} specified");
         }
         $cls = 'Kwf_Form_Field_' . $field['type'];
         if (!class_exists($cls)) {
             throw new Kwf_Exception("Invalid type: Form-Field-Class {$cls} does not exist.");
         }
         $fieldObject = new $cls();
         unset($field['type']);
         foreach ($field as $propName => $propValue) {
             $fieldObject->setProperty($propName, $propValue);
         }
         $this->_form->fields[] = $fieldObject;
     }
     if (!$this->_form->getModel()) {
         if (isset($this->_table)) {
             $this->_form->setTable($this->_table);
         } else {
             if (isset($this->_tableName)) {
                 $this->_form->setTable(new $this->_tableName());
             } else {
                 if (isset($this->_modelName)) {
                     $this->_form->setModel(new $this->_modelName());
                 } else {
                     if (isset($this->_model)) {
                         if (is_string($this->_model)) {
                             $this->_form->setModel(Kwf_Model_Abstract::getInstance($this->_model));
                         } else {
                             $this->_form->setModel($this->_model);
                         }
                     }
                 }
             }
         }
     }
     $this->_initFields();
     $this->_form->initFields();
     $this->_form->trlStaticExecute();
     if (!$this->_form->fields->first() instanceof Kwf_Form_Container_Tabs) {
         $this->_form->setBodyStyle('padding: 10px;');
     }
     if (!$this->_form->getId()) {
         if (is_array($this->_form->getPrimaryKey())) {
             foreach ($this->_form->getPrimaryKey() as $key) {
                 $id[$key] = $this->_getParam($key);
             }
             $this->_form->setId($id);
         } else {
             $this->_form->setId($this->_getParam($this->_form->getPrimaryKey()));
         }
     }
     Kwf_Benchmark::subCheckpoint('init form', microtime(true) - $t);
 }
Esempio n. 4
0
 public function testDeleteWithFieldRows()
 {
     $form = new Kwf_Form();
     $model = new Kwf_Model_FnF(array('dependentModels' => array('Model2' => new Kwf_Model_FieldRows(array('fieldName' => 'data'))), 'data' => array(array('id' => 1, 'data' => serialize(array('data' => array(array('id' => 1, 'foo' => 'foo1', 'pos' => 1), array('id' => 2, 'foo' => 'foo2', 'pos' => 2), array('id' => 3, 'foo' => 'foo3', 'pos' => 3)), 'autoId' => 4))))));
     $form->setModel($model);
     $form->add(new Kwf_Form_Field_MultiFields('Model2'))->fields->add(new Kwf_Form_Field_TextField('foo'));
     $post = array('Model2' => array(array('id' => 1, 'foo' => 'foo1.'), array('id' => 3, 'foo' => 'foo3.')));
     $form->setId(1);
     $post = $form->processInput(null, $post);
     $form->validate(null, $post);
     $form->prepareSave(null, $post);
     $form->save(null, $post);
     $form->afterSave(null, $post);
     $data = $model->getData();
     $data = unserialize($data[0]['data']);
     $data = array_values($data['data']);
     $this->assertEquals(2, count($data));
     $this->assertEquals('foo1.', $data[0]['foo']);
     $this->assertEquals('foo3.', $data[1]['foo']);
 }