Location: PHPKode > scripts > Combo Validation > combo-validation/examples/customizedView.php
<?php
/**
* @package ComboValidation
* @subpackage Examples
*/

require_once('includes.inc.php');
#--(Begin)-->Validation
/**
* @notice all of the options and suboption names are case insensitive
*/
$validation=cmfcValidation::factory('v1',array(
	'formName'=>'myForm',
	'displayMethod'=>'nearFields',
	'displayMethodOptions'=>array(
		'id'=>'messageBoard',
		'backgroundColor'=>'#FFC0C0'
	),
	'displayModesInfo'=>array(
		'customizedDiv'=>array(
			'name'=>'customizedDiv',
			'jsHandler'=>array(
				'function'=>'displayModeCustomizedDiv'
			)
		)
	),
	'fieldsInfo'=>array(
		"contactInfo[name]"=>array(
			'title'=>'Name',
			'type'=>'string',
			'param'=>array(
				'notEmpty'=>true
			)
		),
		"contactInfo[email]"=>array(
			'title'=>'Email',
			'type'=>'email',
			'jsMessageBoardId'=>'emailMessageBoard',
			'param'=>array(
				'notEmpty'=>true
			)
		),
		"contactInfo[subject]"=>array(
			'title'=>'Subject',
			'type'=>'string',
			'param'=>array(
				'notEmpty'=>true,
				'lengthMin'=>10,
				'lengthMax'=>100,
				'regexp'=>'/agreed/i',//php regex with preg_match
				'jsRegexp'=>'/agreed/i',//javascript regex with preg_match
				'regexpDescription'=>' : should contains "agreed" word',
			)
		),
		"contactInfo[description]"=>array(
			'title'=>'Description',
			'type'=>'string',
			'param'=>array(
				'notEmpty'=>true
			)
		),
		"contactInfo[tel]"=>array(
			'title'=>'Tel',
			'type'=>'number',
			'param'=>array(
				'notEmpty'=>true,
				'countMin'=>5,
				'countMax'=>7
			)
		),
		"contactInfo[age]"=>array(
			'title'=>'Age',
			'type'=>'number',
			'param'=>array(
				'notEmpty'=>true,
				'countMin'=>2, //min number of characters
				'countMax'=>2, //max number of characters
				'min'=>18, //min value
				'max'=>60 //max value
			)
		),
		"contactInfo[website]"=>array(
			'title'=>'Website',
			'type'=>'url',
			'param'=>array(
			)
		),
		"contactInfo[subscription]"=>array(
			'title'=>'Subscription',
			'type'=>'checkBox',
			'param'=>array(
				'notEmpty'=>true,
			)
		)
		
	)
));

if ($_GET['mode']=='alert') {
	$validation->setOption('displayMethod','alert');
}
if ($_GET['mode']=='div') {
	$validation->setOption('displayMethod','div');
	$validation->setOption('displayMethodOptions',array('id'=>'messageBoard'));
}
if ($_GET['mode']=='nearFields') {
	$validation->setOption('displayMethod','nearFields');
	//$validation->setOption('displayMethodOptions',array('id'=>'messageBoard'));
}
if ($_GET['mode']=='pageCenterDiv') {
	$validation->setOption('displayMethod','pageCenterDiv');
}
if ($_GET['mode']=='formCenterDiv') {
	$validation->setOption('displayMethod','formCenterDiv');
}
if ($_GET['mode']=='customizedDiv') {
	$validation->setOption('displayMethod','customizedDiv');
}


$successfulSubmit = 0;
$messages='';
if (isset($_POST['submit_send'])){

	/**
	* validating form via package
	*/
	$validateResult=$validation->validate($_POST);
	
	if (empty($validateResult)) {
		$messages[]='Bingo!';
	} else {
		foreach ($validateResult as $r){
			$messages[]=$r->getMessage();
			$successfulSubmit = 0;
		}		
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>Customized View Example</title>
</head>
<body>
	<a href="index.php">Home</a>
	<p>Available Modes :</p>
	<ul>
		<li>alert <a href="?mode=alert">sample...</a></li>
		<li>div <a href="?mode=div">sample...</a></li>
		<li>nearFields <a href="?mode=nearFields">sample...</a></li>
		<li>pageCenterDiv <a href="?mode=pageCenterDiv">sample...</a></li>
		<li>formCenterDiv <a href="?mode=formCenterDiv">sample...</a></li>
		<li>customizedDiv (user's customized mode) <a href="?mode=customizedDiv">sample...</a></li>
	</ul>
	
	<table width="541" border="0" cellspacing="0" cellpadding="0" dir="<?=$dir?>">
		<tr>
			<td class="boxtop" valign="top"  background="interface/images/fullbox_top.gif" style="text-align:<?= $align ?>; direction:<?= $dir ?>;">
	
			</td>
		</tr>
		<tr>
			<td class="boxbody" background="interface/images/fullbox_bg.gif" style="text-align:left; direction:ltr">
				<? if (is_array($messages)) { ?>
				<div align="center">
					<div align="left" style="color:red;padding:3px">
						<?
						foreach($messages as $message){
							echo $message."<br/>";
						}
						?>
					</div>
				</div>
				<? }?>
				
				<div style="margin:5px; text-align:justify">
				</div>
					
				<br />
	
			<? if (!$successfulSubmit) { 
					/**
					* if you only need php validation, you can comment 
					* following 2 line codes
					*/
					$validation->printJsClass();
					$validation->printJsInstance();
			?>
				<script>
					/**
					* javascript for handling customizedDiv display mode
					*/
					function displayModeCustomizedDiv(validation,fieldsInfo,messages,displayModeOptions) {
						var messagesStr='';
						messagesStr='<ul>';
						for (key in messages) {
							fieldInfo=fieldsInfo[key];
							if (messages[key])
								messagesStr+='<li>'+messages[key]+'</li>';
						}
						messagesStr+='</ul>';
						var container=document.getElementById(displayModeOptions['id']);
						container.style.backgroundColor=displayModeOptions['backgroundColor'];
						container.innerHTML=messagesStr;
					}
				</script>
				<div id="messageBoard">
				</div>
				<form name="myForm" action="?<?=cmfcUrl::excludeQueryStringVars(array('pt', 'pageType', 'sectionName'),'get') ?>" method="post" enctype="application/x-www-form-urlencoded">
					<table width="100%" border="0" cellspacing="5" cellpadding="0" style="text-align:<?= $align ?>; direction:<?= $dir ?>">
						<tr>
							<td>Name : </td>
							<td><input class="input" name="contactInfo[name]" value="<?=$_POST['contactInfo']['name']?>" type="text" /></td>
						</tr>	
						<tr>
							<td>Tel : </td>
							<td><input class="input" name="contactInfo[tel]" value="<?=$_POST['contactInfo']['tel']?>" type="text" style="direction:ltr" /></td>
						</tr>
						<tr>
							<td>Age : </td>
							<td><input class="input" name="contactInfo[age]" value="<?=$_POST['contactInfo']['age']?>" type="text" style="direction:ltr" /></td>
						</tr>
						<tr>
							<td>Email : </td>
							<td><input class="input" name="contactInfo[email]" value="<?=$_POST['contactInfo']['email']?>" type="text" />
								<span id="emailMessageBoard" style="padding:3px;background-color:#FFC0C0;color:#800000;display:none"></span>
							</td>
						</tr>
						<tr>
							<td>Website : </td>
							<td><input class="input" name="contactInfo[website]" value="<?=$_POST['contactInfo']['website']?>" type="text" /></td>
						</tr>
						<tr>
							<td>Subject : </td>
							<td><input class="input" name="contactInfo[subject]" value="<?=$_POST['contactInfo']['subject']?>" type="text"  /></td>
						</tr>
						<tr>
							<td>Description : </td>
							<td><textarea class="textarea" name="contactInfo[description]" cols="40" rows="4" ><?=$_POST['contactInfo']['description']?></textarea></td>
						</tr>
						<tr>
							<td>Subscription : </td>
							<td><input class="input" name="contactInfo[subscription]" value="1" <?=($_POST['contactInfo']['subscription']==1)?'checked':''?> type="checkbox"  /></td>
						</tr>
						<tr>
							<td></td>
							<td><input class="button" name="submit_send" type="submit" value=" Send " /></td>
						</tr>
					</table>
				</form>
			<? } ?>
			</td>
		</tr>
	</table>
</body>
</html>
Return current item: Combo Validation