| VDaemon v3 Samples | |
This sample demonstrates two new features of VDaemon v3.x:
- Nested GROUP validator
- validation of submit buttons by javascript
When you run this example you will see one textbox for zip code and three "Country" buttons. To pass validation you must enter valid US, Canada or UK zip code and press appropriate button. All validation is performed by one BIG nested GROUP validator (see source code below). It contains one usual "required" validator and four another "group" validators. GROUP validators can be nested since VDaemon version 3.0.0.
GROUP validator is just logical "OR" or "AND" evaluation of all its subvalidators. In conjunction with logical "NOT" operation available for each "simple" validator ("negation" attribute) it gives you possibility to construct logical expressions of any complexity with your validation rules.
OK, here is the form validation logic:
Form is valid if:
( Some value is entered to "Zip" textbox AND
( this value is valid US zip code OR "US code" button is NOT pressed ) AND
( this value is valid Canada zip code OR "Canada code" button is NOT pressed
) AND
( this value is valid UK zip code OR "UK code" button is NOT pressed ) AND
( "US code" button pressed OR "Canada code" button pressed OR "UK
code" button
pressed )
)
Any parenthesis content above can be converted to GROUP validator. For example expression
( value is valid US zip code OR "US
code" button is NOT pressed )
corresponds to:
<vlgroup operator="OR">
<vlvalidator type="format" format="zip_us" control="Zip">
<vlvalidator type="required" negation="true" control="US">
</vlgroup>
"operator" attribute of <vlgroup> tag defines OR or AND logical operation for all subvalidators (OR by default). "negation" attribute of <vlvalidator> tag defines logical NOT operation for this validator if set to "true". It can be used with any validators except GROUP and CUSTOM.
If you look to the source code you see "setfocus" attribute defined in some validators. VDaemon v3.x automatically sets focus to the form element referenced by "control" attribute of the first invalid validator. But I don't want to set focus to the submit buttons. I prevent this by adding setfocus="false" attribute to the validators which check buttons.
Lastly I added error messages and GROUP validator is ready.
Several words about checking submit buttons. It is not a problem to check whether button pressed on server with PHP. Much more efforts are needed to do this with javascript. Now VDaemon v3.x can check submit buttons in the same way as any other form elements. While digging into the problem I discovered that I can't disable submit button if I need to validate it - browsers don't send data to the server from disabled elements. So, don't use "disablebuttons" attribute of <form> tag in such case.
Limitation: VDaemon can check only <input type="submit"> buttons, but not <input type="image"> or <button type="submit">...</button> elements. It is because different browsers send different data from above elements to server. Do not try to check these elements with VDaemon.
In conclusion I must say that this is not the best sample of how to check zip code. But my goal was to demonstrate power of group validator, not the perfect zip code validation.
<?php include('vdaemon.php'); ?>
<html>
<head>
<title>Group Validator Sample</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="samples.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>Group Validator Sample</h1>
<p>To pass this form validation you must enter valid US, Canada or UK zip code and press appropriate button.</p>
<form action="group_p.php" method="post" id="GroupVal" runat="vdaemon">
<table border="0" cellspacing="0" cellpadding="4">
<tr>
<td>
<vllabel validators="ZipGroup" errclass="error" for="Zip" cerrclass="controlerror">Zip code:</vllabel>
</td>
<td>
<input name="Zip" type="text" class="control" id="Zip" size="10">
</td>
<td valign="top">
<input name="US" type="submit" class="control" id="US" value="US code">
<input name="Canada" type="submit" class="control" id="Canada" value="Canada code">
<input name="UK" type="submit" class="control" id="UK" value="UK code">
</td>
</tr>
<tr>
<td colspan="3">
<vlsummary class="error" headertext="Error(s):" displaymode="bulletlist">
<vlgroup name="ZipGroup" operator="and">
<vlvalidator type="required" control="Zip" errmsg="Zip code required">
<vlgroup operator="or" errmsg="Invalid US code">
<vlvalidator type="required" negation="true" control="US" setfocus="false">
<vlvalidator type="format" format="zip_us" control="Zip">
</vlgroup>
<vlgroup operator="or" errmsg="Invalid Canada code">
<vlvalidator type="required" negation="true" control="Canada" setfocus="false">
<vlvalidator type="format" format="zip_canada" control="Zip">
</vlgroup>
<vlgroup operator="or" errmsg="Invalid UK code">
<vlvalidator type="required" negation="true" control="UK" setfocus="false">
<vlvalidator type="format" format="zip_uk" control="Zip">
</vlgroup>
<vlgroup operator="or" errmsg="You must press to some button">
<vlvalidator type="required" control="US" setfocus="false">
<vlvalidator type="required" control="Canada" setfocus="false">
<vlvalidator type="required" control="UK" setfocus="false">
</vlgroup>
</vlgroup>
</td>
</tr>
</table>
</form>
</body>
</html>
<?php VDEnd(); ?>
<?php
define('VDAEMON_PARSE', false);
include('vdaemon.php');
if (isset($_POST['US'])) {
$sCountry = 'US';
} elseif (isset($_POST['Canada'])) {
$sCountry = 'Canada';
} elseif (isset($_POST['UK'])) {
$sCountry = 'UK';
}
?>
<html>
<head>
<title>Group Validator Sample</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="samples.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>Group Validator Sample</h1>
<p>Your have entered <b><?php echo $sCountry; ?></b> zip code: <b><?php echo $_POST['Zip']; ?></b></p>
</body>
</html>