protected function defaultEvent($aEvent) { $oForm = new weeForm('myform', 'update'); if (!empty($aEvent['post'])) { $aData = $oForm->filter($aEvent['post']); try { $oForm->validate($aData); // Validation success: process the form doSomething($aData); } catch (FormValidationException $e) { $oForm->fill($aData); $oForm->fillErrors($e); } } $this->set('form', $oForm); }
protected function eventEdit($aEvent) { $oResource = exResourceSet::instance()->fetch($aEvent['get']['r']); $oForm = new weeForm('resource', 'edit'); $oForm->fill($oResource); if (isset($aEvent['post'])) { $aData = $oForm->filter($aEvent['post']); try { $oForm->validate($aData); $oResource->setFromArray($aData); $oResource->update(); $this->update('replaceContent', '#msg', 'The resource has been successfully edited!'); } catch (FormValidationException $e) { $this->update('replaceContent', '#msg', 'The submitted data is erroneous!'); $oForm->fillErrors($e); } $oForm->fill($aData); if (array_value($aEvent, 'context') == 'xmlhttprequest') { $this->update('replace', 'form', $oForm); } } $this->set('form', $oForm); }
<?php // Initialization if (!defined('FORM_PATH')) { define('FORM_PATH', dirname(__FILE__) . '/form/'); } // Test $aPost = array('hidden' => 42, 'textbox' => 'error'); $oForm = new weeForm('mini', 'update'); $aFilteredPost = $oForm->filter($aPost); $this->isEqual($aPost, $aFilteredPost, _WT('Valid data which should not have been filtered is missing.')); $this->isEqual($aPost['hidden'], $aFilteredPost['hidden'], _WT('Value for hidden changed.')); $this->isEqual($aPost['textbox'], $aFilteredPost['textbox'], _WT('Value for textbox changed.')); $oForm = new weeForm('mini'); $aFilteredPost = $oForm->filter($aPost); $this->isNotEqual($aPost, $aFilteredPost, _WT('Data should have been filtered.')); $this->isTrue(!isset($aFilteredPost['hidden']), _WT('Value for hidden found in filtered data.')); $this->isEqual($aPost['textbox'], $aFilteredPost['textbox'], _WT('Value for textbox changed.'));
/** Provide values to each widgets of the form. When a value has no corresponding widget, it is discarded. When a value is an empty string '', it is replaced by null. However, when an array contains an empty string, it is left as-is. @param $aData The data used to fill the form's widgets values. */ public function filter($aData) { $aData = parent::filter($aData); foreach ($aData as $sName => $mValue) { if (!is_array($mValue) && strlen($mValue) === 0) { $aData[$sName] = null; } } return $aData; }