validate_set_params

This plugin is a slight extension/mod for SmartyValidate to allow you to set the content that appears before and after each SmartyValidate error message.

Usage Example:

{validate_set_params pre='<p class="validate_error"><img src="/art/warning_clr.gif" alt="Error" /> ' post='</p>'}

That would encapsulate each following error with '<p class="validate_error"><img src="/art/warning_clr.gif" alt="Error" /> ' and '</p>'.

NOTE: this mod required a slight modification of a single line in function.validate.php (from SmartyValidate)

(this diff is against SmartyValidate 2.5)

diff:

--- function.validate_orig.php	2005-07-18 15:41:18.815772352 -0500
+++ function.validate.php	2005-07-18 15:27:09.383905624 -0500
@@ -119,7 +119,7 @@
                     $smarty->append($params['assign'], $_sess['validators'][$_validator_key]['message']);                                        
                 } else {
                     // no assign or append, so echo message
-                    echo $_sess['validators'][$_validator_key]['message'];
+                    echo $_sess['pre'] . $_sess['validators'][$_validator_key]['message'] . $_sess['post'];
                 }
             }
         } else {

or basically,

replace the line : 
echo $_sess['validators'][$_validator_key]['message'];
with :
echo $_sess['pre'] . $_sess['validators'][$_validator_key]['message'] . $_sess['post'];

Example .php code

Please refer to SmartyValidate Docs for its syntax, nothing is different about the .php code

SmartyValidate::register_validator('fname','fname','notEmpty');

Example Output .tpl code

	...
	{validate_set_params pre='<p class="validate_error"><img src="/art/warning_clr.gif" alt="Error" /> ' post='</p>'}
		<fieldset>
			<p class="title">Contact Information</p>
			<div class="row">
				{validate id="fname" message="You must provide a First Name"}
				<label class="required" for="fname">First Name</label><br />
				<input class="large" type="text" name="fname" id="fname" />
			</div>

	...

Example Output

	...
		<fieldset class="new">
			<p class="title">Contact Information</p>
			<div class="row">
				<p class="validate_error"><img src="/art/warning_clr.gif" alt="Error" /> You must provide a First Name</p>
				<label class="required" for="fname">First Name</label><br />
				<input class="large" type="text" name="fnamee" id="fname" />
			</div><br />
	...

Source Listing

<?php
/**
 * Smarty plugin
 * @file function.validate_set_params.php
 * @package Smarty
 * @subpackage plugins
 */
 
/**
 * Smarty {validate_set_params} function plugin
 *
 * Type:     function<br>
 * Name:     validate_set_perams<br>
 * Purpose:  set SmartyValidate parameters
 * @link http://www.phpinsider.com/smarty-forum/viewtopic.php?t=5166
 * @param array
 * @author tclineks < travis dot cline at gmail dot com >
 * @param Smarty
 * @return string
 */
function smarty_function_validate_set_params($params, &$smarty) {

    $_form = isset($params['form']) ? $params['form'] : 'default';
    $_sess =& $_SESSION['SmartyValidate'][$_form];

    if(!SmartyValidate::is_registered_form($_form)) {
        trigger_error("SmartyValidate: [validate plugin] form '$_form' is not registered.");
        return false;
    }

    if (!class_exists('SmartyValidate')) {
        $smarty->trigger_error("validate: missing SmartyValidate class");
        return;
    }
    if (!isset($_SESSION['SmartyValidate'])) {
        $smarty->trigger_error("validate: SmartyValidate is not initialized, use connect() first");
        return;
    }

    // currently only pre and post will pass through to be set but it would be simple/easy to extend further
    $_sess['pre'] = $params['pre'];
    $_sess['post'] = $params['post'];
}

?>