Sådan benyttes komponenten ValidatorClient klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/ValidatorClient.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? ValidatorClient::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new ValidatorClient($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten ValidatorClient klassen
Den fulde PHP kildekode for ValidatorClient klassen
<?php/** * @package validator * @filesource * @see HTML_VALIDATOR_COMPONENT_PATH.'/ValidatorClient.php' * @copyright (c) http://Finn-Rasmussen.com * @license http://Finn-Rasmussen.com/license/ myPHP License conditions * @author http://Finn-Rasmussen.com * @version 1.11 * @since 27-nov-2009 */// TODO may be the specific validator flags should be used instead?// Like VALIDATOR_IS_REQUIRED, VALIDATOR_IS_INT etc./** * The required files */if (defined('HTML_LANGUAGE_UTIL_PATH')) { require_once(HTML_LANGUAGE_UTIL_PATH.'/Translate.php');}/** * The ValidatorClient returns a plug-n-play javascript object * * <code> * Usage: * $validator = new ValidatorClient(); * $script = $validator->newScript($row, $datareader, $classname, $id); * Or * $script = Validator::newScript($row, $datareader, $classname, $id); * Or * Validator::display(); * </code> * @package validator */class ValidatorClient { /** * Constructor */ function __construct() { } /** * Test if the $key is valid to use * Ignore name clashing for db-link and db-image * @param String $key The key to test, if valid * @return True, if this is not a form atttribute, like 'class' or other stuff */ private static function isValid($key) { $isvalid = true; $attribytes = array( 'class', 'title', ); /** * Pseudo column for the db-basket * Ignore this speciel field, which is a combination of Quantity * Price */ if (defined('SELECT_BASKET_SUBTOTAL')) { $attribytes[] = SELECT_BASKET_SUBTOTAL; } if (in_array($key, $attribytes)) { $isvalid = false; } return $isvalid; } /** * Return the new Not Null client code as a javascript snip * which will look like the following javascript code: * <code> * if (theForm.<$name>.value == '') { * isValid = false; * if (focusMe === '') { * focusMe = theForm.<$name>.name; * } * messages += '<$name>'; * } * </code> * @param String $tab The tab, if enabled * @param String $cr The carry return, if enabled * @param $datareader The datareader object * @param String $key The key * @return Raw Return an object where all the stuff is included */ private static function newNotNull($tab, $cr, $datareader, $key) { $script = new Raw(); $isvalid = ValidatorClient::isValid($key); if ($isvalid) { $flags = $datareader->getFieldFlags($key); if ($flags != '') { if (is_array($flags)) { if (in_array(COLUMN_FIELD_FLAG_PRIMARY_KEY, $flags)) { // Ignore } else { if (in_array(COLUMN_FIELD_FLAG_NOT_NULL, $flags)) { $name = $datareader->getFieldName($key); $script->add($tab."if (typeof theForm.".$name." == 'object' && theForm.".$name.".value == '') {".$cr); $script->add($tab.$tab."isValid = false;".$cr); $script->add($tab.$tab."if (focusMe === '') {".$cr); $script->add($tab.$tab.$tab."focusMe = theForm.".$name.".name;".$cr); $script->add($tab.$tab."}".$cr); $script->add($tab.$tab."messages += \"".Translate::sql($name).'\r\n'."\";".$cr); $script->add($tab."}"); } } } else { // Ignore } } else { // Ignore } } else { $script->add($tab."// DEBUG: $key\tNOT possible to add auto validator, because of conflicting form attributes"); } return $script; } /** * Return the new Start client code as a javascript snip * which will look like the following javascript code: * <code> * // Start of onSubmit function * function onSubmit<$classname>(theForm) { * var isValid = true; * var messages = ''; * var focusMe = ''; * : * // see also newEnd() * </code> * @param String $tab The tab, if enabled * @param String $cr The carry return, if enabled * @param String $classname The name of the calling class * @return Raw Return an object where all the stuff is included */ private static function newStart($tab, $cr, $classname) { $script = new Raw(); $script->add("function onSubmit".$classname."(theForm) {".$cr); $script->add($tab."var isValid = true;".$cr); $script->add($tab."var messages = '';".$cr); $script->add($tab."var focusMe = '';".$cr); return $script; } /** * Return the new End client code as a javascript snip * which will look like the following javascript code: * <code> * // see also newStart() * : * if (!isValid) { * var msg = 'text, fill out the form'; * msg += '===================" * msg += messages; * alert(msg); * } else { * theForm.$name.disabled = 'disabled'; // If the submit button have a name * } * if (focusMe !== '') { * theForm[focusMe].focus(); * } * return isValid; * } * // End of onSubmit function * </code> * @param String $tab The tab, if enabled * @param String $cr The carry return, if enabled * @param String $id The ID of the submit button, if any * @return Raw Return an object where all the stuff is included */ private static function newEnd($tab, $cr, $id='') { $script = new Raw(); $script->add($tab."if (!isValid) {".$cr); $script->add($tab.$tab."var msg = \"".VALIDATOR_TEXT_FILL_OUT_THE_FORM.'\r\n'."\";".$cr); $script->add($tab.$tab."msg += '===================".'\r\n'."';".$cr); $script->add($tab.$tab."msg += messages;".$cr); $script->add($tab.$tab."alert(msg);".$cr); /** * If the submit button has an ID, then disable the submit button */ if ($id !== '') { $script->add($tab."} else {\r\n"); $script->add($tab.$tab."var submitId = document.getElementById('$id');".$cr); $script->add($tab.$tab."submitId.value = ' ...';".$cr); $script->add($tab.$tab."submitId.disabled = true;".$cr); $script->add($tab.$tab."submitId.className = '".CSS_BUTTON_SUBMIT_CLASS.' '.CSS_BUTTON_BEE."';".$cr); // CSS_COLOR_DARK CSS_ERROR// DELETE // $script->add($tab.$tab."theForm.$id.value = ' ...';".$cr);// $script->add($tab.$tab."theForm.$id.disabled = true;".$cr);// $script->add($tab.$tab."theForm.$id.className = '".CSS_BUTTON_SUBMIT_CLASS.' '.CSS_BUTTON_BEE."';".$cr); // CSS_COLOR_DARK CSS_ERROR // $script->add($tab.$tab."alert('x='+submitId.className);".$cr); // +submitId.value+' y='+submitId.className } $script->add($tab."}\r\n"); /** * Show alert box, if any messages * Note: The focusMe could be an object or an empty string */ $script->add($tab."if (!isValid && focusMe !== '') {".$cr); $script->add($tab.$tab."theForm[focusMe].focus();".$cr); $script->add($tab."}\r\n"); $script->add($tab."return isValid;".$cr); $script->add("}".$cr); return $script; } /** * Get the javascript onsubmit handler code for fields which must be not_null * The following sample javascript code is generated, for a username field * * // Usage of the form/input * <form ... onsubmit="return onSubmitViewForm(this);"> * <input type="text" name="username" value="" /> * </form> * * // Autogenerated javascript code * function onSubmitViewForm(theForm) { * var isValid = true; * var messages = ''; * if (theForm.username.value == '') { * isValid = false; * messages += 'Username\r\n'; * } * * // more validators, are inserted here * * if (!isValid) { * var msg = 'Fill out the username:\r\n'; * msg += '===================\r\n'; * msg += messages; * alert(msg); * } * if (focusMe !== '') { * theForm[focusMe].focus(); * } * return isValid; * } * * <code> * Usage: * $row = array( * array('firstname'=>'Finn', 'lastname'=>'Rasmussen'), * array('firstname'=>'Zita', 'lastname'=>'Christensen'), * ); * $header = ''; // The header meta data * $default = ''; // The default meta data array * $limit = 10; // The limit to use for the array * $sort = true; * $datareader = DataReaderFactory::newDataReader($row, $header, $default, $limit, $sort) * $classname = "Test"; * $id = "submitButtonID"; * * // The client script is returned, plug-n-play * $script = ValidatorClient::newScript($row, $datareader, $classname, $id); * </code> * * @param array $row The row to popup as a form * @param $datareader The datareader object * @param String $classname The name of the calling class * @param String $id The ID of the submit button, if any * @return Script The script object */ public static function newScript($row, $datareader, $classname, $id='') { $script = new Script(); $cr = ""; $space = ""; $tab = ""; if (defined('DEBUG_LEVEL_SHOW_CSS') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_CSS) { $cr = "\r\n"; $space = " "; $tab = "\t"; } // Start of onSubmit function $script->add(ValidatorClient::newStart($tab, $cr, $classname)); foreach($row as $key=>$value) { if ($key === DATABASE_PREFIX.TABLE_NAME_SESSION.SELECT_TABLE_ID) { // Ignore } else { // Add client code for not null fields $script->add(ValidatorClient::newNotNull($tab, $cr, $datareader, $key));// $type = $datareader->getFieldType($key);// switch ($type) {// case COLUMN_FIELD_TYPE_REAL:// case COLUMN_FIELD_TYPE_INT:// case COLUMN_FIELD_TYPE_CHECKBOX:// case COLUMN_FIELD_TYPE_DATETIME://// print "File:".__FILE__." line:".__LINE__."<br />TODO validate client $type-$key=>$value<br />";// break;// default:// // Ignore//// print "TODO validate client $type-$key=>$value<br />";// break;// // } } } // End of onSubmit function $script->add(ValidatorClient::newEnd($tab, $cr, $id)); return $script; }}?>
Den fulde HTML kildekode for ValidatorClient klassen
<? <!-- DEBUG: Script --> <script type="text/javascript"> //<![CDATA[ function onSubmitValidatorClient(theForm) { var isValid = true; var messages = ''; var focusMe = ''; // DEBUG: 0 NOT possible to add auto validator, because of conflicting form attributes if (!isValid) { var msg = "Udfyld felterne:\r\n"; msg += '===================\r\n'; msg += messages; alert(msg); } if (!isValid && focusMe !== '') { theForm[focusMe].focus(); } return isValid; } //]]> </script> ?>
Her er 'klasse metoderne' for ValidatorClient klassen:
Her er 'objekt variable' for ValidatorClient klassen: