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 testDate()
 {
     $m1 = new Kwf_Model_FnF();
     $form = new Kwf_Form();
     $form->setModel($m1);
     $field1 = $form->add(new Kwf_Form_Field_DateField('test1'));
     $field2 = $form->add(new Kwf_Form_Field_DateField('test2'));
     $field3 = $form->add(new Kwf_Form_Field_DateField('test3'));
     $form->trlStaticExecute();
     $field1->trlStaticExecute();
     $field2->trlStaticExecute();
     $field3->trlStaticExecute();
     $post = array($field1->getFieldName() => '"2009-12-01T00:00:00"', $field2->getFieldName() => trlKwf('yyyy-mm-dd'), $field3->getFieldName() => date(trlKwf('Y-m-d'), strtotime('2010-01-10')));
     $post = $form->processInput($form->getRow(), $post);
     $form->validate($form->getRow(), $post);
     $form->prepareSave(null, $post);
     $form->save(null, $post);
     $form->afterSave(null, $post);
     $row = $m1->getRow($m1->select());
     $this->assertEquals('2009-12-01', $row->{$field1->getFieldName()});
     $this->assertEquals(null, $row->{$field2->getFieldName()});
     $this->assertEquals('2010-01-10', $row->{$field3->getFieldName()});
 }
Exemple #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);
 }