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 $oForm = new weeForm('mini'); $oForm->fillErrors(array('textbox' => 'Bad')); $oForm->fillErrors(array('textbox' => 'Good')); $this->isMatching('/Good/', $oForm->toString(), _WT("The value given to textbox isn't in the generated form."));
<?php // Initialization if (!defined('FORM_PATH')) { define('FORM_PATH', dirname(__FILE__) . '/form/'); } // Test $oForm = new weeForm('mini', 'add'); $this->isEqual(2, count($oForm->xpath('//widget')), _WT('weeForm::xpath returned a wrong count of widgets.')); $oForm = new weeForm('mini', 'update'); $this->isEqual(3, count($oForm->xpath('//widget')), _WT('weeForm::xpath returned a wrong count of widgets.'));
$this->isFalse($oHelper->isSelected(41), _WT('Only 39 should have been selected.')); $this->isFalse($oHelper->isSelected(42), _WT('Only 39 should have been selected.')); $this->isFalse($oHelper->isSelected(43), _WT('Only 39 should have been selected.')); $this->isFalse($oHelper->isSelected(44), _WT('Only 39 should have been selected.')); $oDupeHelper->select('40'); $oHelper->selectNone(); $this->isTrue($oDupeHelper->isSelected(40), _WT('The dupe item should still be selected.')); $oHelper->select(array('41', '44')); $this->isFalse($oHelper->isSelected(39), _WT('Only 41 and 44 should have been selected.')); $this->isFalse($oHelper->isSelected(40), _WT('Only 41 and 44 should have been selected.')); $this->isTrue($oHelper->isSelected(41), _WT('Only 41 and 44 should have been selected.')); $this->isFalse($oHelper->isSelected(42), _WT('Only 41 and 44 should have been selected.')); $this->isFalse($oHelper->isSelected(43), _WT('Only 41 and 44 should have been selected.')); $this->isTrue($oHelper->isSelected(44), _WT('Only 41 and 44 should have been selected.')); $oHelper->selectOne('43'); $this->isFalse($oHelper->isSelected(39), _WT('Only 43 should have been selected.')); $this->isFalse($oHelper->isSelected(40), _WT('Only 43 should have been selected.')); $this->isFalse($oHelper->isSelected(41), _WT('Only 43 should have been selected.')); $this->isFalse($oHelper->isSelected(42), _WT('Only 43 should have been selected.')); $this->isTrue($oHelper->isSelected(43), _WT('Only 43 should have been selected.')); $this->isFalse($oHelper->isSelected(44), _WT('Only 43 should have been selected.')); $oForm = new weeForm('options', 'update'); $oHelper = $oForm->helper('weeFormOptionsHelper', 'choice'); $oHelper->addOption('string'); $this->isTrue($oHelper->isInOptions('string'), _WT('Adding an option as a string does not work.')); $oHelper->addOption(new weeDummyPrintable('printable')); $this->isTrue($oHelper->isInOptions('printable'), _WT('Adding an option as a printable object does not work.')); $oHelper->select('string'); $this->isTrue($oHelper->isSelected('string'), _WT('Selecting an option which has been added as a string does not work.')); $oHelper->selectOne('printable'); $this->isTrue($oHelper->isSelected('printable'), _WT('Selecting an option which has been added as a printable object does not work.'));
<?php $oForm = new weeForm('myform'); echo $oForm->toString();
public function __construct($sFilename, $sAction = 'add') { $this->setUserStylesheetsPath(ROOT_PATH . 'app/xsl'); parent::__construct($sFilename, $sAction); }
<?php // Initialization if (!defined('FORM_PATH')) { define('FORM_PATH', dirname(__FILE__) . '/form/'); } // Test $oForm = new weeForm('mini'); $this->isEqual(2, count($oForm->xpath('//widget')), _WT('weeForm::xpath returned a wrong count of widgets.')); $this->isInstanceOf($oForm->xpathOne('//widget[@type="submitbutton"]'), 'SimpleXMLElement', _WT('weeForm::xpathOne returned something unexpected.')); try { $oForm->xpathOne('//widget'); $this->fail(_WT('weeForm::xpathOne should fail when finding more than 1 result.')); } catch (UnexpectedValueException $e) { } $oForm->removeNodes('//widget[name="hidden"]'); $this->isEqual(0, count($oForm->xpath('//widget[name="hidden"]')), _WT('weeForm::removeNodes does not work.'));
<?php // Initialization if (!defined('FORM_PATH')) { define('FORM_PATH', dirname(__FILE__) . '/form/'); } // Test $oForm = new weeForm('mini'); $oForm->setUserStylesheetsPath(dirname(__FILE__) . '/xslt/'); $this->isMatching('/Test/', $oForm->toString(), _WT("The user stylesheet for submitbutton isn't loaded properly."));
<?php // Initialization if (!defined('FORM_PATH')) { define('FORM_PATH', dirname(__FILE__) . '/form/'); } // Test $oForm = new weeForm('mini', 'update'); $aPost = array('hidden' => 42, 'textbox' => 'error'); try { $oForm->validate($aPost); $this->fail(_WT('Form validation should have failed.')); } catch (FormValidationException $e) { }
<?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; }
<?php $oForm = new weeForm('myform'); $oForm->setUserStylesheetsPath(ROOT_PATH . 'app/xsl');
/** Create a simple upload form and handle everything until the file is uploaded. @param $sDest Destination folder for the file. @param $oRenderer Template where the form is shown. @param $oForm Custom form in case the default one isn't enough. */ function upload_form($sDest, $oRenderer, $oForm = null) { if ($oForm === null) { $oForm = new weeForm('weexlib/upload'); } if (!empty($_FILES)) { try { upload_move_to($sDest); $oRenderer['upload_success'] = true; } catch (Exception $e) { $oForm->fillErrors(array('file' => $e->getMessage())); } } $oRenderer['upload_form'] = $oForm; }
<?php // Initialization if (!defined('FORM_PATH')) { define('FORM_PATH', dirname(__FILE__) . '/form/'); } // Test $oForm = new weeForm('required'); try { $oForm->validate(array()); $this->fail(_WT('Form validation should have failed.')); } catch (FormValidationException $e) { } try { $oForm->validate(array('textbox' => 'testing')); $this->fail(_WT('Form validation should have failed.')); } catch (FormValidationException $e) { } try { $oForm->validate(array('checklist' => array())); $this->fail(_WT('Form validation should have failed.')); } catch (FormValidationException $e) { } try { $oForm->validate(array('textbox' => 'testing', 'checklist' => array())); $this->fail(_WT('Form validation should have failed.')); } catch (FormValidationException $e) { } try { $oForm->validate(array('textbox' => 'testing', 'checklist' => array(0 => 0))); } catch (FormValidationException $e) {