|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
CirTap Smarty Pro
Joined: 04 Jun 2003 Posts: 106
|
Posted: Tue Feb 01, 2005 8:21 pm Post subject: SmartyValidate: PlugIn to check field dependencies |
|
|
Hi,
here we go
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Feb 01, 2005 8:29 pm Post subject: |
|
|
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 |
|
CirTap Smarty Pro
Joined: 04 Jun 2003 Posts: 106
|
Posted: Tue Feb 01, 2005 9:50 pm Post subject: |
|
|
ok, thanks 4 the tip. I'll give it a try.
Have fun,
CirTap |
|
Back to top |
|
CirTap Smarty Pro
Joined: 04 Jun 2003 Posts: 106
|
Posted: Tue Feb 01, 2005 11:54 pm Post subject: |
|
|
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
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
Have fun,
CirTap |
|
Back to top |
|
|
|
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
|
|