cvs: pear /HTML_QuickForm QuickForm.php /HTML_QuickForm/docs QuickForm_example3.php

From: Date: Mon, 09 Jun 2003 15:05:26 +0000
Subject: cvs: pear /HTML_QuickForm QuickForm.php /HTML_QuickForm/docs QuickForm_example3.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-10807@lists.php.net to get a copy of this message
avb Mon Jun 9 11:05:26 2003 EDT Modified files: /pear/HTML_QuickForm/docs QuickForm_example3.php /pear/HTML_QuickForm QuickForm.php Log: * rule-registering functions can now be called statically * added addFormRule() method, changed the example to use that * removed the code to delete uploaded files: PHP does this automatically

Index: pear/HTML_QuickForm/docs/QuickForm_example3.php diff -u pear/HTML_QuickForm/docs/QuickForm_example3.php:1.2 pear/HTML_QuickForm/docs/QuickForm_example3.php:1.3 --- pear/HTML_QuickForm/docs/QuickForm_example3.php:1.2 Tue Feb 18 08:44:50 2003 +++ pear/HTML_QuickForm/docs/QuickForm_example3.php Mon Jun 9 11:05:26 2003 @@ -7,26 +7,27 @@ * @author Bertrand Mansion <bmansion@mamasam.com> * @version 2.0 * -* $Id: QuickForm_example3.php,v 1.2 2003/02/18 13:44:50 mansion Exp $ +* $Id: QuickForm_example3.php,v 1.3 2003/06/09 15:05:26 avb Exp $ */ require_once ('HTML/QuickForm.php'); $form = new HTML_QuickForm(); -function cmpPass($element, $pass2val) +function cmpPass($fields) { - global $form; - $pass1val = $form->getElementValue('passwd1'); - return ($pass1val == $pass2val); + if (strlen($fields['passwd1']) && strlen($fields['passwd2']) && + $fields['passwd1'] != $fields['passwd2']) { + return array('passwd1' => 'Passwords are not the same'); + } + return true; } -$form->registerRule('compare', 'function', 'cmpPass'); $form->addElement('password', 'passwd1', 'Enter password'); $form->addElement('password', 'passwd2', 'Confirm password'); $form->addElement('submit', 'submit', 'submit'); $form->addRule('passwd1', 'Please enter password', 'required'); $form->addRule('passwd2', 'Please confirm password', 'required'); -$form->addRule('passwd2', 'Passwords are not the same', 'compare', 'function'); +$form->addFormRule('cmpPass'); $form->validate(); $form->display(); Index: pear/HTML_QuickForm/QuickForm.php diff -u pear/HTML_QuickForm/QuickForm.php:1.100 pear/HTML_QuickForm/QuickForm.php:1.101 --- pear/HTML_QuickForm/QuickForm.php:1.100 Sat May 31 15:45:50 2003 +++ pear/HTML_QuickForm/QuickForm.php Mon Jun 9 11:05:26 2003 @@ -17,7 +17,7 @@ // | Bertrand Mansion <bmansion@mamasam.com> | // +----------------------------------------------------------------------+ // -// $Id: QuickForm.php,v 1.100 2003/05/31 19:45:50 avb Exp $ +// $Id: QuickForm.php,v 1.101 2003/06/09 15:05:26 avb Exp $ require_once('PEAR.php'); require_once('HTML/Common.php'); @@ -46,6 +46,25 @@ 'html' =>array('HTML/QuickForm/html.php', 'HTML_QuickForm_html') ); +$GLOBALS['_HTML_QuickForm_registered_rules'] = array( + 'required' =>array('regex', '/(\s|\S)/'), + 'maxlength' =>array('regex', '/^(\s|\S){0,%data%}$/'), + 'minlength' =>array('regex', '/^(\s|\S){%data%,}$/'), + 'rangelength' =>array('regex', '/^(\s|\S){%data%}$/'), + 'regex' =>array('regex', '%data%'), + 'email' =>array('regex', '/^[a-zA-Z0-9\._-]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/'), + 'emailorblank' =>array('regex', '/(^$)|(^[a-zA-Z0-9\._-]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$)/'), + 'lettersonly' =>array('regex', '/^[a-zA-Z]+$/'), + 'alphanumeric' =>array('regex', '/^[a-zA-Z0-9]+$/'), + 'numeric' =>array('regex', '/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/'), + 'nopunctuation' =>array('regex', '/^[^().\/\*\^\?#!@$%+=,\"\'><~\[\]{}]+$/'), + 'nonzero' =>array('regex', '/^[1-9][0-9]+/'), + 'uploadedfile' =>array('function', '_ruleIsUploadedFile'), + 'maxfilesize' =>array('function', '_ruleCheckMaxFileSize'), + 'mimetype' =>array('function', '_ruleCheckMimeType'), + 'filename' =>array('function', '_ruleCheckFileName') +); + // {{{ error codes /* @@ -181,6 +200,13 @@ var $_rules = array(); /** + * Form rules, global variety + * @var array + * @access private + */ + var $_formRules = array(); + + /** * Array containing the validation errors * @since 1.0 * @var array @@ -196,40 +222,6 @@ */ var $_requiredNote = '<span style="font-size:80%; color:#ff0000;">*</span><span style="font-size:80%;"> denotes required field</span>'; - /** - * Array of registered element types - * @var array - * @since 1.0 - * @access private - */ - var $_registeredTypes = array(); - - /** - * Array of registered element types - * @var array - * @since 1.0 - * @access private - */ - var $_registeredRules = - array( - 'required' =>array('regex', '/(\s|\S)/'), - 'maxlength' =>array('regex', '/^(\s|\S){0,%data%}$/'), - 'minlength' =>array('regex', '/^(\s|\S){%data%,}$/'), - 'rangelength' =>array('regex', '/^(\s|\S){%data%}$/'), - 'regex' =>array('regex', '%data%'), - 'email' =>array('regex', '/^[a-zA-Z0-9\._-]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/'), - 'emailorblank' =>array('regex', '/(^$)|(^[a-zA-Z0-9\._-]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$)/'), - 'lettersonly' =>array('regex', '/^[a-zA-Z]+$/'), - 'alphanumeric' =>array('regex', '/^[a-zA-Z0-9]+$/'), - 'numeric' =>array('regex', '/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/'), - 'nopunctuation' =>array('regex', '/^[^().\/\*\^\?#!@$%+=,\"\'><~\[\]{}]+$/'), - 'nonzero' =>array('regex', '/^[1-9][0-9]+/'), - 'uploadedfile' =>array('function', '_ruleIsUploadedFile'), - 'maxfilesize' =>array('function', '_ruleCheckMaxFileSize'), - 'mimetype' =>array('function', '_ruleCheckMimeType'), - 'filename' =>array('function', '_ruleCheckFileName') - ); - // }}} // {{{ constructor @@ -250,7 +242,6 @@ $target = (empty($target) || $target == '_self') ? array() : array('target' => $target); $attributes = array('action'=>$action, 'method'=>$method, 'name'=>$formName, 'id'=>$formName) + $target; $this->updateAttributes($attributes); - $this->_registeredTypes =& $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']; $this->_submitValues = $GLOBALS['_' . strtoupper($method)]; $this->_submitFiles =& $_FILES; } // end constructor @@ -304,7 +295,7 @@ */ function registerRule($ruleName, $type, $data1, $data2=null) { - $this->_registeredRules[$ruleName] = array($type, $data1, $data2); + $GLOBALS['_HTML_QuickForm_registered_rules'] = array($type, $data1, $data2); } // end func registerRule // }}} @@ -966,6 +957,29 @@ } // end func addGroupRule // }}} + // {{{ addFormRule() + + /** + * Adds a global validation rule + * + * This should be used when for a rule involving several fields or if + * you want to use some completely custom validation for your form. + * The rule function/method should return true in case of successful + * validation and array('element name' => 'error') when there were errors. + * + * @access public + * @param mixed Callback, either function name or array(&$object, 'method') + * @throws HTML_QuickForm_Error + */ + function addFormRule($rule) + { + if (!$this->_callbackExists($rule)) { + return PEAR::raiseError(null, QUICKFORM_INVALID_RULE, null, E_USER_WARNING, 'Callback function does not exist in HTML_QuickForm::addFormRule()', 'HTML_QuickForm_Error', true); + } + $this->_formRules[] = $rule; + } + + // }}} // {{{ addData() /** @@ -1106,7 +1120,7 @@ */ function isRuleRegistered($name) { - return isset($this->_registeredRules[$name]); + return isset($GLOBALS['_HTML_QuickForm_registered_rules'][$name]); } // end func isRuleRegistered // }}} @@ -1121,7 +1135,7 @@ */ function getRegisteredRules() { - return array_keys($this->_registeredRules); + return array_keys($GLOBALS['_HTML_QuickForm_registered_rules']); } // end func getRegisteredRules // }}} @@ -1371,7 +1385,7 @@ $type = $rule['type']; $message = $rule['message']; $format = $rule['format']; - $ruleData = $this->_registeredRules[$type]; + $ruleData = $GLOBALS['_HTML_QuickForm_registered_rules'][$type]; if ($elementType != 'group') { if (!$this->_validateElement($target, $submitValue, $format, $ruleData)) { @@ -1388,18 +1402,14 @@ } } - if (count($this->_errors) > 0) { - if (is_array($files = $this->_submitFiles)) { - foreach ($files as $file) { - if (isset($file['tmp_name']) && - $this->isUploadedFile($file['tmp_name'])) { - @unlink($file['tmp_name']); - } - } + // process the global rules now + foreach ($this->_formRules as $rule) { + if (true !== ($res = call_user_func($rule, $this->_submitValues, $this->_submitFiles))) { + $this->_errors += $res; } - return false; } - return true; + + return (0 == count($this->_errors)); } // end func validate // }}} @@ -1766,7 +1776,7 @@ $message = $rule['message']; $format = $rule['format']; $reset = (isset($rule['reset'])) ? $rule['reset'] : false; - $ruleData = $this->_registeredRules[$type]; + $ruleData = $GLOBALS['_HTML_QuickForm_registered_rules'][$type]; if ($validation == 'client') { $index = $this->_elementIndex[$elementName]; if ($this->_elements[$index]->getType() == 'group' ||
« previous php.pear.cvs (#10807) next »