File: D:/HostingSpaces/SBogers10/pvg.komma-mediadesign.nl/wwwroot/lib/form/form_field.class.php
<?php
/**
* m_form.php$_info
* Created by Komma Mediadesign.
* Author: mike
* Date: 3/27/13
*/
class Form_Field extends Functions
{
/*
* Error
*/
protected $_error;
/*
* Information about the field
*/
protected $_info = array('type'=>'text','classes'=>array('reset_value'));
/*
* Construct
*/
public function __construct($label = null)
{
$this->_info['label'] = $label;
$this->_info['name'] = $this->camelCase($label);
$this->_info['id'] = $this->_info['name'];
}
/*
* Configure form field
*/
public function config($key,$value)
{
if( ! empty($key) && $value != null)
{
if((isset($this->_info[$key]) && is_array($this->_info[$key]) || is_array($value)))
{
if(is_array($value) && key($value) != 0)
{
foreach($value as $k => $val)
{
$this->_info[$key][$k] = $val;
}
}
if(is_array($value))
{
foreach($value as $k => $val)
{
$this->_info[$key][] = $val;
}
}
else
{
$this->_info[$key][] = $value;
}
}
else
{
$this->_info[$key] = $value;
}
}
}
/*
* Get form configuration
*/
public function get($key = null)
{
if( ! $key == null)
{
return $this->_info[$key];
}
return $this->_info;
}
/*
* Get form configuration
*/
public function set($info)
{
if( ! empty($info)) $this->_info = $info;
}
/*
* Set value of the field
*/
public function defaultValue()
{
if($value = Session::get(array($this->_info['form_name'].'_data',$this->_info['name'])))
{
$this->_info['value'] = $value;
}
else{
$this->_info['value'] = $this->_info['label'];
}
}
/*
* Validate form field
*/
public function validate()
{
// info['validation'] has [@attributes] as first key.
$validation = array();
if(isset($this->_info['validation']))
{
foreach($this->_info['validation'] as $info)
{
if(gettype($info) == 'object')
{
$validation[] = $info->getName();
}
}
}
$this->_info['validation'] = $validation;
if(count($this->_info['validation']) > 0)
{
// Field is required, check if value exists (for checkboxes)
if(in_array('required_isset',$this->_info['validation']))
{
if( ! isset($_POST[$this->_info['name']]))
{
$this->_error = 'Vul het veld “' . $this->_info['label'] . '” in a.u.b. ';
return false;
}
}
// Set value to validate
$input = null;
if(isset($_POST[$this->_info['name']])) $input = $_POST[$this->_info['name']];
// Field is required, check if value is not empty)
if(in_array('required',$this->_info['validation']))
{
if(empty($input) || strip_tags($input) == $this->_info['label'])
{
$this->_error = 'Vul het veld “' . $this->_info['label'] . '” in a.u.b. ';
return false;
}
}
// Check if value is a valid e-mail string
if(in_array('email',$this->_info['validation']))
{
if(!$this->checkMail($input))
{
$this->_error = 'U heeft geen geldig e-mail adres ingevuld. (bv. naam@voorbeeld.nl)';
return false;
}
}
// Specific validation for each object
if(in_array('specified',$this->_info['validation']) && isset($this->_info['object']))
{
$object = $this->_info['object'];
$file = DOCUMENT_ROOT . 'lib/form/types/' . $object . '.class.php';
if(is_file($file))
{
if( ! class_exists($object)) require $file;
$TypeObject = new $object;
if($TypeObject->validate($input))
{
return true;
}
$this->_error = $TypeObject->getError();
}
return false;
}
}
return true;
}
public function getError()
{
return $this->_error;
}
}