Location: PHPKode > scripts > form utils > form-utils/class.form.php
<?

class Form {

	/* *************************
		Dafault Error message */
	var $ERRFormMain			= "This form is incomplete";
	var $ERRFormfieldEmpty		= "[fieldname] is empty";
	var $ERRFormfieldMaxThan		= "[fieldname] : The value you entered is too short. Max is [caractere] chars";
	var $ERRFormfieldMinThan		= "[fieldname] : The value you entered is too long. Min is [caractere] chars";
	var $ERRFormdateinvalid		= "[fieldname] : The date you entered does not exist";
	var $ERRFormemailinvalid		= "[fieldname] : Invalid email address";
	var $ERRFormMinchoice		= "[fieldname] : You should select min [caractere] item";
	var $ERRFormMaxchoice		= "[fieldname] : You should select max [caractere] item";
	var $ERRFormnotnumeric		= "[fieldname] : You have to enter a valid number";
	var $ERRFormValuenotValid	= "[fieldname] : The [caractere] Value is not a valid selection";
	var $DefaultDateType		= "d/m/y";
	var $DefaultDateLib			= "Day/Month/Year";


	function Form($name) {
		$this->FormNname	= $name;
		$this->oFields		= "";
		$this->HasError	= FALSE;
		$this->ListErrMsg	= "";
		$this->StyleField	= "";
	}


	//Build form tag
	function fToTag($onSubmit=""){
		if($onSubmit=="")	$onSubmit="true";

		$FormName =$this->FormNname;
		return "<form name='$FormName' method='post' onSubmit='return $onSubmit;'>";
	}

	function fCreateField($visualName,$reportName,$name,$type,$vmin,$vmax,$mandatory,$multiple) {

		$this->oFields[$name] = $this->lBuildArrField($name,$visualName,$reportName,$type,$vmin,$vmax,$mandatory,$multiple);
	}

	function lBuildArrField($name,$visualName,$reportName,$type,$vmin,$vmax,$mandatory,$multiple) {
		$tArrfield['name']			= $name;
		$tArrfield['visualName']		= $visualName;
		$tArrfield['reportName']		= $reportName;
		$tArrfield['type']			= $type;
		$tArrfield['min']			= $vmin;
		$tArrfield['max']			= $vmax;
		$tArrfield['mandatory']		= $mandatory;
		$tArrfield['multiple']		= $multiple;
		$tArrfield['default']		= "";
		$tArrfield['list']			= "";
		$tArrfield['Handles']		= "";
		$tArrfield['value']			= "";
		$tArrfield['iserror']		= "";
		return $tArrfield;
	}



/*	***************************
	functions to design fields */

	function fToField($field="",$size="") {
		$maxVal 		= $this->oFields[$field]['max'];
		$defaulVal	= $this->oFields[$field]['value'];

		$tevents= $this->lapplyHandles($this->oFields[$field]['Handles']);

		return "<input type=\"text\" name=\"$field\" size=\"$size\" value=\"$defaulVal\" maxlength=\"$maxVal\" $tevents>";
	}

	function fToHidden($field="") {
		$defaulVal	= $this->oFields[$field]['value'];
		return "<input type=\"hidden\" name=\"$field\" value=\"$defaulVal\" >";
	}

	function fToArea($field="",$col="", $row="", $warp="") {

		$defaulVal	= $this->oFields[$field]['value'];
		$tevents= $this->lapplyHandles($this->oFields[$field]['Handles']);
		return "<textarea name=\"$field\" cols=\"$col\" rows=\"$row\" $tevents>$defaulVal</textarea>";
	}

	function fToDate($field="",$size="") {
		$tevents		= "";
		$ListOption	= "";
		$treturn		= "";
		$tevents		= $this->lapplyHandles($this->oFields[$field]['Handles']);

		$dateType = explode("/",$this->DefaultDateType);			// Array type value (d, m, y)
		$dateLib	= explode("/",$this->DefaultDateLib);			// Array Libelle value (Day, Month, Year)
		$dateVal	= explode("/",$this->oFields[$field]['value']);	// Array Value (01, 01, 1900)
		for ($d=0; $d<=count($dateType)-1; $d++){

			$fieldname = $this->oFields[$field]['name']."_".$dateType[$d]."[]";

			if ($dateType[$d]=="d") {
				$Option	= $this->laddfieldoption(1, 31, $dateLib[$d], $dateVal[$d]);
			}
			elseif ($dateType[$d]=="m") {
				$Option	= $this->laddfieldoption(1, 12, $dateLib[$d], $dateVal[$d]);
			}
			elseif ($dateType[$d]=="y") {
				$Option	= $this->laddfieldoption($this->oFields[$field]['min'], $this->oFields[$field]['max'], $dateLib[$d], $dateVal[$d]);
			}

			$treturn .="<select name=\"$fieldname\" size=\"$size\" $tevents>$Option</select>&nbsp;";
		}
		return $treturn;
	}

	function laddfieldoption($tmin="", $tmax="", $tstart="", $tvalue="") {
		$treturn="";

		if ($tstart<>"")	$treturn.= "<option value=\"\">$tstart</option>";

		for ($i=$tmin; $i<=$tmax; $i++) {
			$selected = "";
			if ($tvalue==$i)	$selected = "selected";
			$treturn.= "<option value=\"$i\" $selected>$i</option>";
		}
		return $treturn;
	}

	function fToSelect($field="",$size="") {
		$tevents		= "";
		$ListOption	= "";
		$tevents		= $this->lapplyHandles($this->oFields[$field]['Handles']);

		if ($size>1) 	{$multiple = "multiple";} else {$multiple = "";}

		// get alls values for this field
		while (list ($key, $val) = each ($this->oFields[$field]['list'])) {
			$selected="";

			// if this value is a default value
			if ( ( is_array($this->oFields[$field]['value']) ) and (in_array($key, $this->oFields[$field]['value'])) ) {
				$selected="selected";
			}
			else {
				if ($this->oFields[$field]['value']==$key)	$selected="selected";
			}

			// add option to field "select"
			$ListOption.="<option value=\"$key\" $selected>$val</option>";
		}

		$fieldname = $field."[]";
		return "<select name=\"$fieldname\" size=\"$size\" $tevents $multiple>$ListOption</select>";
	}

	function fMaxOption($field="") {
		return $this->oFields[$field]['list'];
	}

	function fToBox($field="", $n="") {
		$treturn="";
		if ($this->oFields[$field]['list']<>"") {

			// if this field is multiple selection (checkbox or radio)
			if ($this->oFields[$field]['multiple']) { $Objtype = "checkbox";} else { $Objtype = "radio";}

			// get default(s) value(s)
			$tmparray =array_slice($this->oFields[$field]['list'],$n,1);
			while ( list ($key ,$val) = each ($tmparray) ) {
				$keys = $key;
				$vals = $val;
			}
			$checked = "";
			if ( ( is_array($this->oFields[$field]['value']) ) and (in_array($keys, $this->oFields[$field]['value'])) ) {
				$checked="checked";
			}

			$tevents		= $this->lapplyHandles($this->oFields[$field]['Handles']);

			$fieldname = $field."[]";
			$treturn = "<input type=\"$Objtype\" name=\"$fieldname\" value=\"$keys\" $checked $tevents>&nbsp;$vals";
		}
		return $treturn;
	}


	/*
		external function
		display text before field	*/
	function fVisualName($field="") {
		$treturn		= "";
		$applyfont	= "";

		$applyfont = $this->lGetStyleDisplay($field);

		$treturn = str_replace("<%source%>", $this->oFields[$field]['visualName'], $applyfont);

		return $treturn;
	}

	/*
		external function
		add a default value */
	function fAddDefault($field="",$Value="") {
		if ($this->oFields[$field]['type']=="GROUP") {
			$this->oFields[$field]['value'][] = $Value;
		}
		else {
			$this->oFields[$field]['value'] = $Value;
		}
	}

	/*
		external function
		add a option */
	function faddOption($field="", $key="", $val="") {
		$this->oFields[$field]['list'][$key] = $val;
	}

	/*
		external function
		add a handler (style; javascript action, ...) */
	function faddHandles($field="", $type="", $val="") {
		$this->oFields[$field]['Handles'][$type] = $val;
	}

	/*
		internal function
		get all handlers to apply on field
		*/
	function lapplyHandles($handles="") {
		$treturn = "";
		while (list ($key, $val) = each ($handles)) {
			$treturn.= " $key=\"$val\"";
		}
		return $treturn;
	}


	/* ***************
		functions to valid form after post */

	function fDisplayError($field="") {
		$treturn = "";
		if ($this->oFields[$field]['iserror']) {
			$treturn = $this->ListErrMsg[$field];
		}
		return $treturn;
	}

	function lGetStyleDisplay($field="") {
		$applyfont = "";
		if ($this->oFields[$field]['mandatory'])	$applyfont = $this->StyleField['mandatory'];
		if (!$this->oFields[$field]['mandatory'])	$applyfont = $this->StyleField['nomandatory'];
		if ($this->oFields[$field]['iserror'])		$applyfont = $this->StyleField['error'];

		return $applyfont;
	}

	function AddError($obj="", $ErrMsg="") {
		$this->ListErrMsg[$obj]= $ErrMsg;
		$this->oFields[$obj]['iserror']=TRUE;
	}

	function lfieldempty($obj) {
		$msgError = $this->ERRFormfieldEmpty;
		$msgError	= $this->lbuildMsgError($msgError, $this->oFields[$obj]['reportName'], $this->oFields[$obj]['min']);

		$this->AddError($obj, $msgError);
	}

	function linvalidDate($obj="") {
		$msgError = $this->ERRFormdateinvalid;
		$msgError	= $this->lbuildMsgError($msgError, $this->oFields[$obj]['reportName'], "");
		$this->AddError($obj, $msgError);
	}

	function lMinValide($obj, $val) {

		if ($this->oFields[$obj]['type']=="GROUP") {
			$msgError	=	$this->ERRFormMinchoice;
			$mval = $val;
		}
		else {
			$msgError	=	$this->ERRFormfieldMinThan;
			$mval = strlen($val);
		}

		if ( intval($mval) < intval($this->oFields[$obj]['min']) ) {
			$msgError	= $this->lbuildMsgError($msgError, $this->oFields[$obj]['reportName'], $this->oFields[$obj]['min']);

			$this->AddError($obj, $msgError);
		}
	}

	function lMaxValide($obj, $val) {

		if ($this->oFields[$obj]['type']=="GROUP") {
			$msgError	=	$this->ERRFormMaxchoice;
			$mval = $val;
		}
		else {
			$msgError	=	$this->ERRFormfieldMaxThan;
			$mval = strlen($val);
		}


		if ( intval($mval) > intval($this->oFields[$obj]['max']) ) {
			$msgError	= $this->lbuildMsgError($msgError, $this->oFields[$obj]['reportName'], $this->oFields[$obj]['max']);

			$this->AddError($obj, $msgError);
		}
	}

	function lTextfieldValide($objfield,$val){
		$this->lMinValide( $objfield, $val );
		$this->lMaxValide( $objfield, $val );
	}


	function lGroupfieldValide($objfield,$val) {
		$this->lMinValide( $objfield, $val );
		$this->lMaxValide( $objfield, $val );

		if (in_array("", $this->oFields[$objfield]['value'])){
			$Value = $this->oFields[$objfield]['list'][""];
			$msgError = $this->ERRFormValuenotValid;
			$msgError	= $this->lbuildMsgError($msgError,$this->oFields[$objfield]['reportName'],"\"$Value\"");
			$this->AddError($objfield, $msgError);
		}
	}

	function lfieldnumeric($obj, $val) {
		if (is_numeric($val)<>1) {
			$msgError = $this->ERRFormnotnumeric;
			$msgError = $this->lbuildMsgError($msgError, $this->oFields[$obj]['reportName'], "\"dd\"");

			$this->AddError($obj, $msgError);
		}
	}

	function ldatefieldValide($obj="", $val="") {
		if (($val['m']=="") or ($val['d']=="") or ($val['y']=="") ) {
			$this->linvalidDate($obj, $val);
		}
		else {
			$this->lcheckDate($obj, $val);
		}
	}

	function lcheckDate($obj="", $val="") {
		if (!checkdate($val['m'],$val['d'],$val['y']) ){
			$this->linvalidDate($obj, $val);
		}

	}

	function lbuildMsgError($msg="", $fieldReport="", $value="") {
		$msg	=	str_replace("[fieldname]",	$fieldReport,	$msg);
		$msg	=	str_replace("[caractere]",	$value,		$msg);
		return $msg;
	}

	function lValidcheckmail($obj="",$val="") {
         if( !isset( $val ) || !ereg( "^[^@ ]+@[^@ ]+\.[^@ \.]+$", $val ) ) {
			$msgError = $this->lbuildMsgError($this->ERRFormemailinvalid, $this->oFields[$obj]['reportName'], "");

			$this->AddError($obj, $msgError);
         }
	}

	function fValidForm($http="") {

		if (!empty($http)) {

			// Assign values
			while (list ($key ,$val) = each ($this->oFields)) {
				switch ($val['type']) {
					case "EMAIL_FIELD":
						$this->fAddDefault($key,$http[$key]);
						if ($val['mandatory']) {
							if (strlen($http[$key])==0) {
								$this->lfieldempty($key);
							}
							else {
								$this->lValidcheckmail($key,$http[$key]);
							}
						}
						break;

					case "GROUP":

						if (isset ($http[$key]) ) {
							// Add to default values user selected
							$tmpa	= "";
							while (list ($keys ,$vals) = each ($http[$key])) {
								$tmpa[]= $vals;
							}
							$this->oFields[$key]['value']=$tmpa;

							// Valid Field
							if ( $this->oFields[$key]['mandatory']) {
								if (isset ($http[$key]) ) {
									$this->lGroupfieldValide($key,count($http[$key]));
								}
								else {
									$this->lfieldempty($key);
								}
							}
						}
						break;

					case "DATE_FIELD":
						$tmpVal	="";
						$cDate	= "";
						$tmpVal['m']=$http[$this->oFields[$key]['name']."_m"][0];
						$tmpVal['d']=$http[$this->oFields[$key]['name']."_d"][0];
						$tmpVal['y']=$http[$this->oFields[$key]['name']."_y"][0];
						$this->ldatefieldValide($key,$tmpVal);

						$tmpA = explode("/",$this->DefaultDateType);
						for ($i=0; $i<=count($tmpA)-1; $i++) {
							if ( $i==( count($tmpA)-1 ) ) {$sep="";} else {$sep="/";}
							$cDate .= $tmpVal[$tmpA[$i]].$sep;
						}
						$this->fAddDefault($key,$cDate);

						break;

					case "NUMERIC_FIELD":
						$this->fAddDefault($key,$http[$key]);
						if ($val['mandatory']) {
							if ($http[$key]=="") {
								$this->lfieldempty($key);
							}
							else {
								$this->lfieldnumeric($key,$http[$key]);
							}
						}
						break;

					case "TEXT_FIELD":
						$this->fAddDefault($key,$http[$key]);
						if ($val['mandatory']) {
							if ($http[$key]=="") {
								$this->lfieldempty($key);
							}
							else {
								$this->lTextfieldValide($key,$http[$key]);
							}
						}
						break;
				}

			}
		}
	}

// end class
}

?>
Return current item: form utils