<?php /** * Uses the FormHandler Validator class to validate fields on the fly * * @package FormHandler * @author Johan Wiegel * @since 04-12-2008 */ // includedir must be set //echo 'validate'; //print_r( $_REQUEST ); if (isset($_REQUEST['includedir'])) { // to classes needed, can the be found? if (file_exists($_REQUEST['includedir'] . 'includes/class.Validator.php') and file_exists($_REQUEST['includedir'] . 'includes/class.AjaxValidator.php')) { include $_REQUEST['includedir'] . 'includes/class.Validator.php'; include $_REQUEST['includedir'] . 'includes/class.AjaxValidator.php'; $oAjaxValidator = new AjaxValidator(); echo $oAjaxValidator->Validate($_REQUEST, new Validator()); } else { echo 'AJAX validation will not work, classes can not be found, check FH_INCLUDE_DIR in config.inc.php'; } } else { echo 'Something went wrong.'; }
/** * dbFormHandler::flush() * * prints or returns the form * * @return string: the form or null when the form should be printed * @access public * @author Teye Heimans */ function flush($return = false) { if ($this->_ajaxValidator === true) { require_once FH_INCLUDE_DIR . 'includes/class.AjaxValidator.php'; $oAjaxValidator = new AjaxValidator($this->_ajaxValidatorScript); $oAjaxValidator->CreateObservers($this); } // when the form is not posted or the form is not valid if (!$this->isPosted() || !$this->isCorrect()) { // check if a value is set of an unknown field if (sizeof($this->_buffer) > 0) { // error messages for the values for unknown fields foreach ($this->_buffer as $sField => $a) { trigger_error('Value set of unknown field "' . $sField . '"', E_USER_WARNING); } } // get the form $form = $this->_getForm(); } else { if ($this->_curPage < $this->_pageCounter) { // upload and convert uploads $this->_handleUploads(); // get the next form $form = $this->_getForm($this->_curPage + 1); } else { // upload and convert uploads $this->_handleUploads(); // generate the data array $data = array(); foreach ($this->_fields as $name => $fld) { if (is_object($fld[1]) && method_exists($fld[1], 'getValue') && $name != $this->_name . '_submit') { $data[$name] = $fld[1]->getValue(); } } // add the user added data to the array $data = array_merge($data, $this->_add); // call the users oncorrect function if (!empty($this->_onCorrect)) { if (is_array($this->_onSaved)) { $hideForm = call_user_func_array(array(&$this->_onCorrect[0], $this->_onCorrect[1]), array($data, &$this)); } else { $hideForm = call_user_func_array($this->_onCorrect, array($data, &$this)); } } // add the user added data again to the array (could have been changed!) $data = array_merge($data, $this->_add); // if the db option is used if (!is_null($this->_db) && !empty($this->_table) && (!isset($hideForm) || $hideForm)) { // save the data into the databse $id = $this->_saveDbData($data); // query error ? if ($id === -1) { // something went wrong with the query.. display the form again $hideForm = false; } else { // got id back ? if (is_array($id) && sizeof($id) == 1) { $id = $id[0]; } // call the onsaved function if (!is_null($this->_onSaved)) { if (is_array($this->_onSaved)) { $hideForm = call_user_func_array(array(&$this->_onSaved[0], $this->_onSaved[1]), array($id, $data, &$this)); } else { $hideForm = call_user_func_array($this->_onSaved, array($id, $data, &$this)); } } } } // display the form again if wanted.. if (isset($hideForm) && $hideForm === false) { $form = $this->_getForm(); } else { if (isset($hideForm) && is_string($hideForm)) { $form = $hideForm; } else { $form = ''; } } } } // cache all the fields values for the function value() foreach ($this->_fields as $fld => $value) { // check if it's a field if (is_object($this->_fields[$fld][1]) && method_exists($this->_fields[$fld][1], "getvalue")) { $this->_cache[$fld] = $this->_fields[$fld][1]->getValue(); } } /* // remove all vars to free memory foreach( get_object_vars($this) as $name => $value ) { // remove all vars except these ones.. if( !in_array($name, array('_cache', 'edit', 'insert', '_posted', '_name' ) ) ) { unset( $this->{$name} ); } }*/ // disable our error handler! if (FH_DISPLAY_ERRORS) { restore_error_handler(); } // return or print the form if ($return) { return $form; } else { echo $form; return null; } }
function AjaxValidator($bScript = true) { self::$bScript = $bScript; }