Location: PHPKode > scripts > Combo Validation > combo-validation/examples/simpleUsage.php
<?php
/**
* @desc 
* @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',
	//'errorDisplayMethod'=>'alert',
	'fieldsInfo'=>array(
		"contactInfo[name]"=>array(
			'title'=>'Name',
			'type'=>'string',
			'param'=>array(
				'notEmpty'=>true
			)
		),
		"contactInfo[email]"=>array(
			'title'=>'Email',
			'type'=>'email',
			'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,
			)
		)
		
	)
));

/**
* overriding the options is possible via following code
*/
//$validation->setOption('optionName','optionValue');
/**
* for multilingual website you can change this messages easily
* @notice do not include following code into class initializing array, because messages name (definitions)
* 			define after object initializing
*/
$validation->setOption('messagesValue',array(
	CMF_ValidationV1_Error=>'Unknown error',
	CMF_ValidationV1_Is_Not_Valid_Email=>'"__value__" in __title__ is not valid email',
	CMF_ValidationV1_Is_Not_Valid_Url=>'"__value__" in __title__ is not valid url',
	CMF_ValidationV1_Is_Not_Number=>'"__value__" in __title__ is not number',
	CMF_ValidationV1_Is_Not_Within_Range=>'"__value__" of __title__ is not within this range (__min__,__max__)',
	CMF_ValidationV1_Is_Not_Within_Count_Range=>'"__value__" of __title__ is not within this count range (__min__,__max__)',
	CMF_ValidationV1_Is_Empty=>'__title__ value should not be empty',
	CMF_ValidationV1_Is_Not_Selected=>'__title__ is not selected',
	CMF_ValidationV1_Is_Not_Within_Length_Range=>'"__value__" of __title__ is not within this length range (__min__,__max__)',
	CMF_ValidationV1_Is_Not_String=>'__title__ is not string',
	CMF_ValidationV1_Is_Not_Match_With_Pattern=>'__title__ is not match with __desc__',
	CMF_ValidationV1_Field_Does_No_Exists=>'__title__ field "__fieldName__" does not exists'
));
#--(End)-->Validation


$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>Simple Usage Example</title>
</head>
<body>
	<a href="index.php">Home</a>
	<?php

	?>
	<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();
			?>
				<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" /></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