Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

SmartyValidate: PlugIn to check field dependencies

 
Post new topic   Reply to topic    Smarty Forum Index -> Add-ons
View previous topic :: View next topic  
Author Message
CirTap
Smarty Pro


Joined: 04 Jun 2003
Posts: 106

PostPosted: Tue Feb 01, 2005 8:21 pm    Post subject: SmartyValidate: PlugIn to check field dependencies Reply with quote

Hi,
here we go Smile
I searched the forum for this but with no result.
Is there a built-in way to validate a field in dependency of another?
Code:
[ ] call me
_______ phone number

if the "call me" check box is checked, the phone number must be notEmpty.
I don't want to validate the phone number which is a useless task as this may be an intl. number in any possibe format available on this planet. Just needs to have a value.

Thanks.

Have fun,
CirTap

** edited post title


Last edited by CirTap on Tue Feb 01, 2005 11:59 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7367
Location: Lincoln Nebraska, USA

PostPosted: Tue Feb 01, 2005 8:29 pm    Post subject: Reply with quote

This can easily be done with a custom validator. The date and password validators compare different fields so you could look into those for examples.
Back to top
View user's profile Send private message Visit poster's website
CirTap
Smarty Pro


Joined: 04 Jun 2003
Posts: 106

PostPosted: Tue Feb 01, 2005 9:50 pm    Post subject: Reply with quote

ok, thanks 4 the tip. I'll give it a try.

Have fun,
CirTap
Back to top
View user's profile Send private message Visit poster's website
CirTap
Smarty Pro


Joined: 04 Jun 2003
Posts: 106

PostPosted: Tue Feb 01, 2005 11:54 pm    Post subject: Reply with quote

here it is:
[php:1:56c2e610fd]<?php

/**
* Project: SmartyValidate: Form Validator for the Smarty Template Engine
* File: validate_criteria.isEqual.php
* Author: Monte Ohrt <monte@ispi.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @link http://www.phpinsider.com/php/code/SmartyValidate/
* @copyright 2001-2004 ispi of Lincoln, Inc.
* @author Monte Ohrt <monte@ispi.net>
* @package SmartyValidate
* @version 2.3-dev
*/

/**
* makes a field mandatory if "field2" is not empty or contains a value in "matches".
* for comparison the values are casted to strings.
* Synopsis:
* - {validate field="txtFon" criteria="dependsOn" field2="chkCallback"}
* any value of "chkCallback" will make our field mandatory
*
* - {validate field="txtFon" criteria="dependsOn" field2="chkCallback" matches="y,yes,1" ...}
* any value of "y","yes",or "1" for "chkCallback" makes our field mandatory
*
* - {validate field="txtFon" criteria="dependsOn" field2="chkCallback" matches=$varArray ...}
* as previous, but takes the values of the given array for conditions
*
* @author CirTap <ct@otherone.org>
* @param string $value the value being tested
* @param boolean $empty if field can be empty
* @param array params validate parameter values
* @param array formvars form var values
*/
function smarty_validate_criteria_dependsOn($value, $empty, &$params, &$formvars) {
if(!isset($params['field2'])) {
trigger_error("SmartyValidate: [dependsOn] parameter 'field2' is missing.");
return false;
}
if(!isset($formvars[$params['field2']]) || strlen($formvars[$params['field2']]) == 0)
return true;

if (!isset($params['matches'])) {
$matches = array($formvars[$params['field2']]);
} else if (!is_array($params['matches'])) {
$matches = explode(',', $params['matches']);
} else {
$matches =& $params['matches'];
}
if (count($matches)) array_walk($matches, create_function('&$i,$k', 'settype($i, "string");'));
$hit = in_array((string)$formvars[$params['field2']], $matches);

return ($hit && (strlen($value) != 0));
}

?>[/php:1:56c2e610fd]

example template fragment
Code:
{validate field="txtFon" criteria="dependsOn" field2="chkCallback" assign="err_txtFon" message="buggy"}

{if $err_txtFon == "buggy"}<span class="lblReq">Your phone no. is required for call back.</span>{/if}

<input class="txt small" type="text" name="txtFon" size="25" maxlength="45" value="{$smarty.post.txtFon|escape}" />

<input type="checkbox" name="chkCallback" value="Y" {if $smarty.post.chkCallback ne ""}checked="true"{/if} onclick="this.form.elements['txtCallback'].disabled=markReq(this,'txtFon')" />&call back</td>

When: <input class="txt medium" type="text" disabled="true" name="txtCallback" size="25" maxlength="45" value="{$smarty.post.txtCallback|escape}" />


the optional JS onclick() script used to visualize req. on the client
[php:1:56c2e610fd]<script language="JavaScript" type="text/javascript">{literal}
function markReq(elt, destEltName) {
var lbl = getElementWithId(destEltName + '_lbl');
if (elt.checked) {
addClass(lbl, 'lblReq');
if (elt.form.elements[destEltName].value == '') {
elt.form.elements[destEltName].focus();
}
} else {
removeClass(lbl, 'lblReq');
}
return !elt.checked;
}
/* className functions by Dean Edwards 2004.10.24
renamed some vars to conform with my conventions ,-) */
function addClass(elt, className) {
if (!hasClass(elt, className)) {
if (elt.className) elt.className += " " + className;
else elt.className = className;
}
}
function removeClass(elt, className) {
var regexp = new RegExp("(^|\\s)" + className + "(\\s|$)");
elt.className = elt.className.replace(regexp, "$2");
}
function hasClass(elt, className) {
var regexp = new RegExp("(^|\\s)" + className + "(\\s|$)");
return regexp.test(elt.className);
}
{/literal}</script>[/php:1:56c2e610fd]
and of course a CSS class Smile
Code:
.lblReq { color:rgb(159,20,56); }


Feedback is welcome.
Feel free to add this to your plug-in set if you wish.
@Monte: you have my permission to add this to the distrib if you want Smile

Have fun,
CirTap
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Add-ons All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP