SHARE
TWEET

Untitled

a guest Sep 26th, 2011 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /**
  4.  * Project:     SmartyValidate: Form Validator for the Smarty Template Engine
  5.  * File:        function.validate.php
  6.  * Author:      Monte Ohrt <monte at newdigitalgroup dot com>
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Lesser General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2.1 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Lesser General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Lesser General Public
  19.  * License along with this library; if not, write to the Free Software
  20.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21.  *
  22.  * @link http://www.phpinsider.com/php/code/SmartyValidate/
  23.  * @copyright 2001-2005 New Digital Group, Inc.
  24.  * @author Monte Ohrt <monte at newdigitalgroup dot com>
  25.  * @package SmartyValidate
  26.  */
  27.  
  28. function smarty_function_validate($params, &$smarty) {
  29.  
  30.     $_init_params = $smarty->getTemplateVars('validate_init');
  31.  
  32.     if(isset($_init_params)) {
  33.         $params = array_merge($_init_params, $params);
  34.     }
  35.    
  36.     static $_halt = array();
  37.     static $_is_init = null;
  38.     static $_form = null;
  39.     static $_local_is_init = null;
  40.  
  41.     $_form = SmartyValidate::$form;
  42.  
  43.     if(isset($params['form']))
  44.     {
  45.        if($params['form'] != $_form)
  46.           $_is_init = null;
  47.        $_form = $params['form'];
  48.     }
  49.  
  50.     $_sess =& $_SESSION['SmartyValidate'][$_form];
  51.  
  52.     if(!isset($_is_init)) {
  53.         $_is_init = $_sess['is_init'];
  54.     }
  55.    
  56.     if(!isset($_local_is_init))
  57.     {
  58.       $_local_is_init = $_is_init;
  59.     }
  60.  
  61.     if(!SmartyValidate::is_registered_form($_form)) {
  62.         trigger_error("SmartyValidate: [validate plugin] form '$_form' is not registered.");
  63.         return false;
  64.     }    
  65.    
  66.     if(isset($_halt[$_form]) && $_halt[$_form])
  67.         return;    
  68.    
  69.     if (!class_exists('SmartyValidate')) {
  70.         $smarty->trigger_error("validate: missing SmartyValidate class");
  71.         return;
  72.     }
  73.     if (!isset($_SESSION['SmartyValidate'])) {
  74.         $smarty->trigger_error("validate: SmartyValidate is not initialized, use connect() first");
  75.         return;        
  76.     }
  77.    
  78.     if(isset($params['id'])) {
  79.         if (($_validator_key = SmartyValidate::is_registered_validator($params['id'], $_form)) === false) {
  80.             $smarty->trigger_error("validate: validator id '" . $params['id'] . "' is not registered.");
  81.             return;        
  82.         }
  83.     } else {
  84.         if (strlen($params['field']) == 0) {
  85.             $smarty->trigger_error("validate: missing 'field' parameter");
  86.             return;
  87.         }
  88.         if (strlen($params['criteria']) == 0) {
  89.             $smarty->trigger_error("validate: missing 'criteria' parameter");
  90.             return;
  91.         }
  92.     }
  93.     if(isset($params['trim'])) {
  94.         $params['trim'] = SmartyValidate::_booleanize($params['trim']);  
  95.     }
  96.     if(isset($params['empty'])) {
  97.         $params['empty'] = SmartyValidate::_booleanize($params['empty']);
  98.     }
  99.     if(isset($params['halt'])) {
  100.         $params['halt'] = SmartyValidate::_booleanize($params['halt']);
  101.     }
  102.                
  103.     if(isset($_sess['validators']) && is_array($_sess['validators'])) {
  104.         if(isset($params['id'])) {
  105.           if($_local_is_init) {
  106.             $_sess['validators'][$_validator_key]['message'] = $params['message'];
  107.           }
  108.         } else {
  109.             foreach($_sess['validators'] as $_key => $_field) {
  110.                 if($_field['field'] == $params['field']
  111.                     && $_field['criteria'] == $params['criteria']) {
  112.                     // field exists
  113.                     $_validator_key = $_key;
  114.                     break;
  115.                 }
  116.             }
  117.         }
  118.        
  119.         if(!$_local_is_init) {
  120.  
  121.             if(!$_sess['is_error']) // no validation error
  122.                 return;
  123.        
  124.             if(!isset($_sess['validators'][$_validator_key]['valid']) || !$_sess['validators'][$_validator_key]['valid']) {
  125.                 // not valid, show error and reset
  126.                 $_halt[$_form] = isset($_sess['validators'][$_validator_key]['halt'])
  127.                         ? $_sess['validators'][$_validator_key]['halt']
  128.                         : false;
  129.                 $_echo = true;
  130.                 if(isset($params['assign'])) {
  131.                     $smarty->assign($params['assign'], $_sess['validators'][$_validator_key]['message']);                  
  132.                 } elseif (isset($params['append'])) {
  133.                     $smarty->append($params['append'], $_sess['validators'][$_validator_key]['message']);                                        
  134.                 } else {
  135.                     // no assign or append, so echo message
  136.                     echo $_sess['validators'][$_validator_key]['message'];
  137.                 }
  138.             }
  139.         } else {
  140.             if(isset($params['id'])) {
  141.                 $_sess['validators'][$_validator_key] =
  142.                     array_merge($_sess['validators'][$_validator_key], $params);
  143.             } else {
  144.                 $_params = $params;
  145.                 $_params['valid'] = false;
  146.                 $_sess['validators'][] = $_params;
  147.             }
  148.         }
  149.     }
  150.    
  151.     $_sess['is_init'] = false;
  152. }
  153.  
  154. ?>
  155.  
  156.  
RAW Paste Data
Top