Scripts

Class: Formulier Generator & Validator

Features: - Genereren van een formulier - Valideren van een formulier Update - Validatie ingebouwd - setActie() aan toegevoegd ( in formulier klasse ) - Alternatief gebruikt voor filter_var mocht de PHP versie geen PHP 5.2 zijn Tips en verbeteringen zijn altijd welkom!

class-formulier-generator-en-validator
[b]formulier.class.php[/b]
<?php
############################################
#     Formulier Generator & Validator      #
#------------------------------------------#
#  Filename   : formulier.class.php        #
#  Written By : Thijs Ferket ( RdR )       #
#  Website    : www.ferket.net             #
#  Date       : 1-2-2008                   #
#  Version    : 1.2                        #
#------------------------------------------#
############################################

/**
Omschrijving:
Een aantal Klasses om formulieren te genereren en te valideren, xHTML valid
*/

define('TAB', "\t");

ini_set('display_errors', 1);
error_reporting(E_ALL);

/**
* Interface voor alle velden / buttons
*/
interface iPrint {
	public function __toString();
}

/**
* Text Class
*/
class Text implements iPrint {

	protected $sLabel,
			$sNaam,
			$iMaxGrootte,
			$sWaarde,
			$sValidatie;
	
	/**
	* Constructor van Text
	* @param String $sLabel, label van het veld
	* @param String $sNaam, naam (name & id) van het veld
	* @param Integer $iMaxGrootte, de maximale grootte van het veld
	* @param String $sWaarde, inhoud (value) van het veld
	*/
	public function  __construct( $sLabel, $sNaam , $iMaxGrootte = 30, $sWaarde = '' ) {
		if( empty($sLabel) || !is_string($sLabel) ) {
			throw new InvalidArgumentException( '$sLabel moet een String zijn' );
		}
		elseif( empty($sNaam) || !is_string($sNaam) ) {
			throw new InvalidArgumentException( '$sNaam moet een String zijn' );
		}
		elseif( !is_int($iMaxGrootte) ) {
			throw new InvalidArgumentException( '$iMaxGrootte moet een Integer zijn' );
		}
		elseif( !is_string($sWaarde) ) {
			throw new InvalidArgumentException( '$sWaarde moet een String zijn' );
		}
		else {
			$this->sLabel = $sLabel;
			$this->sNaam = $sNaam;
			$this->iMaxGrootte = $iMaxGrootte;
			if( isset($_POST[$this->sNaam]) ) {
				$this->sWaarde = trim(strip_tags($_POST[$this->sNaam]));
			}
			else {
				$this->sWaarde = $sWaarde;
			}
		}
	}
	
	/**
	* Publieke methodeom de naam te retourneren
	* @return String $sNaam
	*/
	public function getNaam() {
		return $this->sNaam;
	}
	
	/**
	* Publieke methode om de valiatie van een veld te setten
	* @param String validatie
	*/
	public function setValidatie( $sValidatie ) {
		$this->sValidatie = $sValidatie;
	}
	
	/**
	* Publieke methode om te kijken of het veld correct ingevuld is
	* @return boolean
	*/
	public function isValid() {
		if( isset($_POST[$this->sNaam]) && $this->sValidatie != null ) {
			$oValidator = new Validator();
			if( is_array($this->sValidatie) ) {
				if( !$oValidator->{$this->sValidatie[0]}($this->sWaarde, $this->sValidatie[1] ) ) {
					$this->sWaarde = '';
					return false;
				}
			}
			elseif( !$oValidator->{$this->sValidatie}($this->sWaarde) ) {
				$this->sWaarde = '';
				return false;
			}
			$oValidator = null;
		}
		return true;
	}	
	
	/**
	* Publieke methode om de html te retourneren
	* @return String $html
	*/
	public function __toString() {
		$class = '';
		if( !$this->isValid() ) {
			$class = 'class="fout"';
		}
		$html = TAB.'<label for="'.$this->sNaam.'" class="floated">'.$this->sLabel.'</label>'.PHP_EOL;
		$html .= TAB.'<input type="text" id="'.$this->sNaam.'" name="'.$this->sNaam.'" maxlength="'.$this->iMaxGrootte.'" value="'.$this->sWaarde.'" '.$class.' /><br />'.PHP_EOL.PHP_EOL;
		return $html;
	}
}

/**
* Password Class
*/
class Password extends Text implements iPrint {
	/**
	* Publieke methode om de html te retourneren
	* @return String $html
	*/
	public function __toString() {
		$class = '';
		if( !$this->isValid() ) {
			$class = 'class="fout"';
		}
		$html = TAB.'<label for="'.$this->sNaam.'" class="floated">'.$this->sLabel.'</label>'.PHP_EOL;
		$html .= TAB.'<input type="password" id="'.$this->sNaam.'" name="'.$this->sNaam.'" maxlength="'.$this->iMaxGrootte.'" value="'.$this->sWaarde.'" '.$class.' /><br />'.PHP_EOL.PHP_EOL;
		return $html;
	}
}

/**
* Hidden Class
*/
class Hidden extends Text implements iPrint {
	/**
	* Publieke methode om de html te retourneren
	* @return String
	*/
	public function __toString() {
		return TAB.'<input type="hidden" id="'.$this->sNaam.'" name="'.$this->sNaam.'" value="'.$this->sWaarde.'" />'.PHP_EOL.PHP_EOL;
	}
}

/**
* File Class
*/
class File extends Text implements iPrint {
	/**
	* Publieke methode om de html te retourneren
	* @return String
	*/
	public function __toString() {
		return TAB.'<label for="'.$this->sNaam.'" class="floated">'.$this->sLabel.'</label>'.PHP_EOL.TAB.'<input type="file" id="'.$this->sNaam.'" name="'.$this->sNaam.'" /><br />'.PHP_EOL.PHP_EOL;
	}
}

/**
* Checkbox Class
*/
class Checkbox implements iPrint {
	protected $sWaarde,
			$bChecked,
			$bVerplicht; 
	
	/**
	* Constructor van Checkbox
	* @param String $sLabel, label van de checkbox
	* @param String $sNaam, naam van de checkbox
	* @param String $sWaarde, de waarde van de checkbox
	* @param Boolean $bChecked, TRUE wanneer de checkbox standaard gechecked moet worden
	*/
	public function  __construct(  $sLabel, $sNaam , $sWaarde, $bChecked = FALSE ) {
		if( empty($sLabel) || !is_string($sLabel) ) {
			throw new InvalidArgumentException( '$sLabel moet een String zijn' );
		}
		elseif( empty($sNaam) || !is_string($sNaam) ) {
			throw new InvalidArgumentException( '$sNaam moet een String zijn' );
		}
		elseif( empty($sWaarde) || !is_string($sWaarde) ) {
			throw new InvalidArgumentException( '$sWaarde moet een String zijn' );
		}
		elseif( !is_bool($bChecked) ) {
			throw new InvalidArgumentException( '$bChecked moet een Boolean zijn, TRUE voor checked en FALSE voor unchecked' );
		}
		else {
			$this->sLabel = $sLabel;
			$this->sNaam = $sNaam;
			$this->sWaarde = $sWaarde;
			if( isset($_POST[$this->sNaam]) ) {
				$this->bChecked = true;
			}
			else {
				$this->bChecked = $bChecked;
			}
			$this->bVerplicht = false;
		}
	}
	
	/**
	* Publieke methodeom de naam te retourneren
	* @return String $sNaam
	*/
	public function getNaam() {
		return $this->sNaam;
	}
	
	/**
	* Publieke methode om het veld verplicht te setten
	*/
	public function setVerplicht() {
		$this->bVerplicht = true;
	}
		
	/**
	* Publieke methode om te kijken of het veld correct ingevuld is
	* @return boolean
	*/
	public function isValid() {
		if( $this->bVerplicht ) {
			if( !isset($_POST[$this->sNaam]) ) {
				return false;
			}
		}
		return true;
	}
	
	/**
	* Publieke methode om de html te retourneren
	* @return String $html
	*/
	public function __toString() {
		$html = TAB.'<input type="checkbox" id="'.$this->sNaam.'" name="'.$this->sNaam.'" value="'.$this->sWaarde.'"';
		if( $this->bChecked ) {
			$html .= ' checked="checked"';
		}
		$html .= ' /><label for="'.$this->sNaam.'">'.$this->sLabel.'</label><br />'.PHP_EOL.PHP_EOL;
		return $html;
	}
}

/**
* Radio Class
*/
class Radio implements iPrint {
	protected $sLabel,
			$sNaam,
			$aWaarde,
			$iChecked,
			$bVerplicht;
	
	/**
	* Constructor van Radio
	* @param String $sLabel, label van de radiobutton
	* @param String $sNaam, naam van de radiobutton
	* @param Array $aWaarde, Array met alle mogelijke keuzes
	* @param Integer $iChecked, array key van welke radiobutton standaard gechecked moet zijn
	*/
	public function __construct(  $sLabel, $sNaam, $aWaarde, $iChecked = 0 ) {
		if( empty($sLabel) || !is_string($sLabel) ) {
			throw new InvalidArgumentException( '$sLabel moet een String zijn' );
		}
		elseif( empty($sNaam) || !is_string($sNaam) ) {
			throw new InvalidArgumentException( '$sNaam moet een String zijn' );
		}
		elseif( empty($aWaarde) || !is_array($aWaarde) ) {
			throw new InvalidArgumentException( '$sWaarde moet een Array zijn' );
		}
		elseif( !is_int($iChecked) ) {
			throw new InvalidArgumentException( '$iChecked moet een Integer zijn tussen de 1 en de lengte van de Array' );
		}
		else {
			if( $iChecked > count($aWaarde) ) {
				$iChecked = 1;
			}
			if( isset($_POST[$sNaam]) ) {
				$iChecked = $_POST[$sNaam];
			}
			$this->sLabel = $sLabel;
			$this->sNaam = $sNaam;
			$this->aWaarde = $aWaarde;
			$this->iChecked = $iChecked;
			$this->bVerplicht = false;
		}
	}
	
	/**
	* Publieke methodeom de naam te retourneren
	* @return String $sNaam
	*/
	public function getNaam() {
		return $this->sNaam;
	}
	
	/**
	* Publieke methode om het veld verplicht te setten
	*/
	public function setVerplicht() {
		$this->bVerplicht = true;
	}
		
	/**
	* Publieke methode om te kijken of het veld correct ingevuld is
	* @return boolean
	*/
	public function isValid() {
		if( $this->bVerplicht ) {
			if( !isset($_POST[$this->sNaam]) ) {
				return false;
			}
		}
		return true;
	}
	
	/**
	* Publieke methode om de html te retourneren
	* @return String $html
	*/
	public function __toString() {
		$html = TAB.'<fieldset>'.PHP_EOL;
		$html .= TAB.'<legend>'.$this->sLabel.'</legend>'.PHP_EOL;
		$i = 1;
		foreach( $this->aWaarde as $sWaarde ) {
			$html .= TAB.'<input type="radio" id="'.$this->sNaam.$i.'" name="'.$this->sNaam.'" value="'.$i.'"';
			if($i == $this->iChecked) {
				$html .= ' checked="checked"';
			}
			$html .= ' /><label for="'.$this->sNaam.$i.'">'.$sWaarde.'</label><br />'.PHP_EOL;
			$i++;
		}
		$html .= TAB.'</fieldset><br />'.PHP_EOL.PHP_EOL;
		return $html;
	}
}

/**
* Select Class
*/
class Select extends Radio implements iPrint {	
	/**
	* Publieke methode om te kijken of het veld correct ingevuld is
	* @return boolean
	*/
	public function isValid() {
		if( $this->bVerplicht ) {
			if( !isset($_POST[$this->sNaam]) || $_POST[$this->sNaam] < 1) {
				return false;
			}
		}
		return true;
	}
	
	/**
	* Publieke methode om de html te retourneren
	* @return String $html
	*/
	public function __toString() {
		$html = TAB.'<label class="floated">'.$this->sLabel.'</label>'.PHP_EOL;
		$html .= TAB.'<select id="'.$this->sNaam.'" name="'.$this->sNaam.'">'.PHP_EOL;
		$html .= TAB.TAB.'<option value="0"> - Selecteer - </option>'.PHP_EOL;
				
		$i = 1;
		foreach( $this->aWaarde as $sWaarde ) {
			$html .= TAB.TAB.'<option value="'.$i.'"';
			if($i == $this->iChecked) {
				$html .= ' selected="selected"';
			}
			$html .= ' >'.$sWaarde.'</option>'.PHP_EOL;
			$i++;
		}
		$html .= TAB.'</select><br />'.PHP_EOL;
		return $html;
	}
}

/**
* TextArea Class
*/
class TextArea implements iPrint {
	protected $sLabel,
			$sNaam,
			$iKolommen,
			$iRijen,
			$sWaarde,
			$sValidatie;
	
	/**
	* Constructor van TextArea
	* @param String $sLabel, label van de textarea
	* @param String $sNaam, naam van de textarea
	* @param Integer $iKolommen, aantal kolommen van de textarea
	* @param Integer $iRijen, aantal rijen van de textarea
	* @param String $sWaarde, inhoud van de textarea
	*/
	public function __construct(  $sLabel, $sNaam, $iKolommen = 50, $iRijen = 20, $sWaarde = '' ) {
		if( empty($sLabel) || !is_string($sLabel) ) {
			throw new InvalidArgumentException( '$sLabel moet een String zijn' );
		}
		elseif( empty($sNaam) || !is_string($sNaam) ) {
			throw new InvalidArgumentException( '$sNaam moet een String zijn' );
		}
		elseif( !is_int($iRijen) ) {
			throw new InvalidArgumentException( '$iKolommen moet een integer zijn' );
		}
		elseif( !is_int($iKolommen) ) {
			throw new InvalidArgumentException( '$iRijen moet een integer zijn' );
		}
		elseif( !is_string($sWaarde) ) {
			throw new InvalidArgumentException( '$sWaarde moet een String zijn' );
		}
		else {
			$this->sLabel = $sLabel;
			$this->sNaam = $sNaam;
			$this->iKolommen = $iKolommen;
			$this->iRijen = $iRijen;
			$this->sWaarde = $sWaarde;
			if( isset($_POST[$this->sNaam]) ) {
				$this->sWaarde = trim(strip_tags($_POST[$this->sNaam]));
			}
			else {
				$this->sWaarde = $sWaarde;
			}
		}
	}
	
	/**
	* Publieke methodeom de naam te retourneren
	* @return String $sNaam
	*/
	public function getNaam() {
		return $this->sNaam;
	}
	
	/**
	* Publieke methode om de valiatie van een veld te setten
	* @param String validatie
	*/
	public function setValidatie( $sValidatie ) {
		$this->sValidatie = $sValidatie;
	}
	
	/**
	* Publieke methode om te kijken of het veld correct ingevuld is
	* @return boolean
	*/
	public function isValid() {
		if( isset($_POST[$this->sNaam]) && $this->sValidatie != null ) {
			$oValidator = new Validator();
			if( !$oValidator->{$this->sValidatie}($this->sWaarde) ) {
				$this->sWaarde = '';
				return false;
			}
			$oValidator = null;
		}
		return true;
	}
	
	/**
	* Publieke methode om de html te retourneren
	* @return String
	*/
	public function __toString() {
		$class = '';
		if( !$this->isValid() ) {
			$class = 'class="fout"';
		}
		return TAB.'<label for="'.$this->sNaam.'" class="floated">'.$this->sLabel.'</label><br />'.PHP_EOL.TAB.'<textarea id="'.$this->sNaam.'" name="'.$this->sNaam.'" rows="'.$this->iRijen.'" cols="'.$this->iKolommen.'" '.$class.'>'.$this->sWaarde.'</textarea><br />'.PHP_EOL.PHP_EOL;
	}
}

/**
* Submit Class
*/
class Submit implements iPrint {
	protected $sNaam,
			$sWaarde;
		
	/**
	* Constructor van Submit
	* @param String $sNaam, naam van het veld
	* @param String $sWaarde, inhoud van het veld
	*/	
	public function __construct( $sNaam, $sWaarde ) {
		if( empty($sNaam) || !is_string($sNaam) ) {
			throw new InvalidArgumentException( '$sNaam moet een String zijn' );
		}
		elseif( empty($sWaarde) || !is_string($sWaarde) ) {
			throw new InvalidArgumentException( '$sWaarde moet een String zijn' );
		}
		else {
			$this->sNaam = $sNaam;
			$this->sWaarde = $sWaarde;
		}
	}

	/**
	* Publieke methode om de html te retourneren
	* @return String
	*/
	public function __toString() {
		return TAB.'<input type="submit" id="'.$this->sNaam.'" name="'.$this->sNaam.'" value="'.$this->sWaarde.'" />'.PHP_EOL;
	}
}

/**
* Reset Class
*/
class Reset extends Submit implements iPrint {
	/**
	* Publieke methode om de html te retourneren
	* @return String
	*/
	public function __toString() {
		return TAB.'<input type="reset" id="'.$this->sNaam.'" name="'.$this->sNaam.'" value="'.$this->sWaarde.'" />'.PHP_EOL;
	}
}

/**
* Formulier Class
*/
class Formulier
{
	protected $sTitel,
			$aVelden,
			$aKnoppen,
			$sActie,
			$aFouten;
	
	/**
	* Constructor die van $aVelden en $aKnoppen een array maakt
	* @param String titel, de titel van het veld
	*/
	public function __construct( $sTitel ) {
		if( !is_string($sTitel) ) { 
			throw new InvalidArgumentException( '$sTitel moet een String zijn' );
		}
		else {
			$this->sTitel = $sTitel;
			$this->aVelden = array();
			$this->aKnoppen = array();
			$this->sActie = $_SERVER['PHP_SELF'];
			$this->aFouten = array();
		}
	}
	
	/**
	* Publieke methode om objecten toe te voegen aan het formulier
	* @param Object $object, het object dat word toegevoegd
	* @param String $sValidatie, de naam van de methode voor de validatie uit de Validator klasse
	*/
	public function add( iPrint $object, $sValidatie = null ) {
		if( $sValidatie != null && $sValidatie != 'verplicht') {
			if( is_array($sValidatie) ) {
				if( !method_exists('Validator', $sValidatie[0]) ) {
					throw new InvalidArgumentException( 'Deze validatie methode bestaat niet.' );
				}
			}
			elseif( !method_exists('Validator', $sValidatie) ) {
				throw new InvalidArgumentException( 'Deze validatie methode bestaat niet.' );
			}
		}
		if( $object instanceof Submit ) {
			$this->aKnoppen[] = $object;
		}
		else {
			$this->aVelden[] = $object;
			if( $object instanceof Radio || $object instanceof Checkbox || $object instanceof Select ) {
				if( strtolower($sValidatie) == 'verplicht' ) {
					$object->setVerplicht();
				}
			}
			else {
				$object->setValidatie( $sValidatie );
			}
		}
	}
	
	/**
	* Prive methode om het enctype te bepalen van het formulier
	* @return String, het enctype van het formulier
	*/
	private function getEnctype() {
		foreach( $this->aVelden as $oVeld ) {
			if( $oVeld instanceof File ) {
				return ' enctype="multipart/form-data"';
			}
		}
	}
	
	/**
	* Publieke methode om de actie van het formulier te setten
	* @param String $sActie, de actie van het formulier
	*/
	public function setActie( $sActie ) {
		$this->sActie = $sActie;
	}
	
	/**
	* Publieke methode om te kijken of alle velden correct zijn ingevuld
	* @return boolean
	*/
	public function controleerVelden() {
		foreach( $this->aVelden as $oVeld ) {
			if( !$oVeld->isValid() ) {
				$this->aFouten[] = 'Het veld "'.$oVeld->getNaam().'" is niet correct ingevuld.';
			}
		}
		if( count($this->aFouten) > 0 ) {
			return false;
		}
		else {
			return true;
		}
	}
	
	/**
	* Publieke methode om alle foutmeldingen te retourneren
	* @return String $html
	*/
	public function getFoutmelding() {
		$aantalFouten = count( $this->aFouten );
		$html = '<div class="fout">'.PHP_EOL;
		if($aantalFouten == 1) {
			$html .= TAB.'De volgende fout is opgetreden:'.PHP_EOL.TAB.'<ul>'.PHP_EOL;
		}
		else {
			$html .= TAB.'De volgende '.$aantalFouten.' fouten zijn opgetreden:'.PHP_EOL.TAB.'<ul>'.PHP_EOL;
		}
		foreach($this->aFouten as $sFout) {
			$html .= TAB.TAB.'<li>'.$sFout.'</li>'.PHP_EOL;
		}
		$html .= TAB.'</ul>'.PHP_EOL.'</div>'.PHP_EOL.PHP_EOL;
		return $html;
	}

	/**
	* Publieke methode om alle velden en knoppen om te zetten naar HTML
	* @return String $html
	*/
	public function __toString() {
		$html = '';
		if( count($this->aFouten) > 0 ) {
			$html .= $this->getFoutmelding();
		}
		$html .= '<h2>'.$this->sTitel.'</h2>'.PHP_EOL;
		$html .= '<form method="post" action="'.$this->sActie.'"'.$this->getEnctype().'>'.PHP_EOL;
		foreach( $this->aVelden as $oVeld ) {
			$html .= $oVeld;
		}
		foreach( $this->aKnoppen as $oKnop ) {
			$html .= $oKnop;
		}
		$html .= TAB.'</form>'.PHP_EOL;
		return $html;
	}
}

/**
* Validator Class
*/
class Validator
{
	/**
	* Een methode om te bekijken of een waarde leeg is
	* @param String $sWaarde
	* @return boolean
	*/
	public function notEmpty( $sWaarde ) {
		return !empty($sWaarde);
	}
	
	/**
	* Een methode om te kijken of een waarde een correct email adres bevat
	* @param String $sWaarde
	* @return boolean
	*/
	public function isEmail( $sWaarde ) {
		if( !empty($sWaarde) ) {
			if( function_exists('filter_var') ) {
				return filter_var($sWaarde, FILTER_VALIDATE_EMAIL);
			}
			else {
				return preg_match('/^[a-z0-9_\.-]+@([a-z0-9]+([\-]+[a-z0-9]+)*\.)+[a-z]{2,7}$/i', $sWaarde);
			}
		}
		else {
			return false;
		}
	}
	
	/**
	* Een methode om te kijken of een waarde een correcte url bevat
	* @param String $sWaarde
	* @return boolean
	*/
	public function isUrl( $sWaarde ) {
		if( !empty($sWaarde) ) {
			if( function_exists('filter_var') ) {
				return filter_var($sWaarde, FILTER_VALIDATE_URL);
			}
			else {
				return preg_match('/^((http|ftp|https):\/{2})?(([0-9a-zA-Z_-]+\.)+[a-zA-Z]+)((:[0-9]+)?)((\/([0-9a-zA-Z=%\.\/_-]+)?(\?[0-9a-zA-Z%\/&=_-]+)?)?)$/', $sWaarde);
			}
		}
		else {
			return false;
		}
	}
	
	/**
	* Een methode om te kijken of een waarde een string is
	* @param String $sWaarde
	* @return boolean
	*/
	public function isAlnum( $sWaarde ) {
		if( !empty($sWaarde) ) {
			return ctype_alnum($sWaarde);
		}
		else {
			return false;
		}
	}
	
	/**
	* Een methode om te kijken of een waarde alleen uit nummers bestaat
	* @param String $sWaarde
	* @return boolean
	*/
	public function isDigit( $sWaarde ) {
		if( !empty($sWaarde) ) {
			return ctype_digit($sWaarde);
		}
		else {
			return false;
		}
	}
	
	/**
	* Een methdoe om te kijken of een waarde een correcte postcode bevat
	* @param String $sWaarde
	* @return boolean
	*/
	public function isPostcode( $sWaarde ) {
		if( !empty($sWaarde) ) {
			return preg_match('/^[1-9]{1}[0-9]{3}\s?[A-Za-z]{2}$/', $sWaarde);
		}
		else {
			return false;
		}
	}
	
	/**
	*
	* @param String $sWaarde
	* @return boolean
	*/
	public function isTel( $sWaarde ) {
		if( !empty($sWaarde) ) {
			return preg_match('/^(\d{2}-?\d{8}|d{3}-?\d{7}|\d{4}-?\d{6})$/', $sWaarde);
		}
		else {
			return false;
		}
	}
	
	/**
	*
	* @param String $sWaarde
	* @param Integer $iLengte
	* @return boolean
	*/
	public function minLengte( $sWaarde, $iLengte ) {
		if( strlen($sWaarde) >= $iLengte ) {
			return true;
		}
		else {
			return false;
		}
	}
	
	/**
	*
	* @param String $sWaarde
	* @param Integer $iLengte
	* @return boolean
	*/
	public function maxLengte( $sWaarde, $iLengte ) {
		if( strlen($sWaarde) <= $iLengte ) {
			return true;
		}
		else {
			return false;
		}
	}		
	
	/**
	*
	* @param String $sWaarde
	* @param Integer $iLengte
	* @return boolean
	*/
	public function isLengte( $sWaarde, $iLengte ) {
		if( strlen($sWaarde) == $iLengte ) {
			return true;
		}
		else {
			return false;
		}
	}		
}
?>

[b]voorbeeld[/b]
[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>	
	<title>Formulier Generator & Validator</title>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<meta http-equiv="Content-Language" content="nl" />
    <style>
	body {
		font: 11px Verdana;
		padding: 20px;
	}
	label.floated {
		float: left;
		width: 140px;
		text-align: left;
		padding-top: 5px;
	}
	fieldset {
		border: 1px solid #565656;
		margin: 10px;
		padding: 5px 20px;
	}
	legend {
		padding: 0 10px;
	}
	input, textarea, select {
		padding: 3px;
		margin: 3px;
		border: 1px solid #000;
		font: 11px Verdana, Arial, Helvetica, sans-serif;
	}
	input.fout, textarea.fout, select.fout {
		border: 1px solid #F60;
	}
	div.fout {
		padding: 14px 10px 15px 40px;
		margin: 5px 0 10px;
		background-repeat: no-repeat;
		background-position: 10px 10px;
		color: #000000;
		border: 1px solid #F00;
		background-color: #F9E1E1; 
	}
	</style>
</head>
<body>

<?php
require_once('formulier.class.php');

try
{	
	$oForm = new Formulier('Aanmelden');
	$oForm->add( new Text('Gebruikersnaam:', 'naam', 40), 'isAlnum' );
	$oForm->add( new Text('Email:', 'email', 255), 'isEmail' );
	$oForm->add( new Text('Postcode:', 'postcode', 6), 'isPostcode' );
	$oForm->add( new Text('Niet verplicht veld:', 'nietverplicht') );
	$oForm->add( new Password('Wachtwoord:', 'wachtwoord', 40), array('minLengte', 5) );
	$oForm->add( new Radio('Geslacht:', 'geslacht', array('Man', 'Vrouw') ), 'verplicht' );
	$oForm->add( new Select( 'Internet Snelheid:', 'internetsnelheid', array('Heel snel', 'Snel', 'Normaal', 'Langzaam' ) ), 'verplicht' );
	$oForm->add( new Checkbox('<b>Ja</b> ik ga akkoord met de voorwaarden.', 'voorwaarden', '1'), 'verplicht' );
	$oForm->add( new Submit('aanmelden', 'Aanmelden') );
	$oForm->add( new Reset('wis', 'Wis velden') );

	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		if( $oForm->controleerVelden() ) {
			echo 'Formulier is correct ingevuld.';
			echo '<pre>' . htmlentities (print_r ($_POST, true)) . '</pre>';
		}
	}
	echo $oForm;
}
catch( Exception $e ) 
{
	echo '<pre>'.$e->getMessage().'</pre>';
}
?>
[/code]

[b]Output[/b]
[code]
<h2>Aanmelden</h2>

<form method="post" action="/classes/formulier.php">
	<label for="naam" class="floated">Gebruikersnaam:</label>
	<input type="text" id="naam" name="naam" maxlength="40" value=""  /><br />

	<label for="email" class="floated">Email:</label>
	<input type="text" id="email" name="email" maxlength="255" value=""  /><br />

	<label for="postcode" class="floated">Postcode:</label>
	<input type="text" id="postcode" name="postcode" maxlength="6" value=""  /><br />

	<label for="wachtwoord" class="floated">Wachtwoord:</label>
	<input type="password" id="wachtwoord" name="wachtwoord" maxlength="40" value=""  /><br />

	<fieldset>
	<legend>Geslacht:</legend>
	<input type="radio" id="geslacht1" name="geslacht" value="1" /><label for="geslacht1">Man</label><br />
	<input type="radio" id="geslacht2" name="geslacht" value="2" /><label for="geslacht2">Vrouw</label><br />

	</fieldset><br />

	<label class="floated">Internet Snelheid:</label>
	<select id="internetsnelheid" name="internetsnelheid">
		<option value="0"> - Selecteer - </option>
		<option value="1" >Heel snel</option>
		<option value="2" >Snel</option>

		<option value="3" >Normaal</option>
		<option value="4" >Langzaam</option>
	</select><br />
	<input type="checkbox" id="voorwaarden" name="voorwaarden" value="1" /><label for="voorwaarden"><b>Ja</b> ik ga akkoord met de voorwaarden.</label><br />

	<input type="submit" id="aanmelden" name="aanmelden" value="Aanmelden" />
	<input type="reset" id="wis" name="wis" value="Wis velden" /
</form>
[/code]

Reacties

0
Nog geen reacties.