File: D:/HostingSpaces/SBogers10/tops.komma.pro/wwwroot/lib/form/form_field.class.php
<?php
/**
* Form_Field.class.php
* Created by Komma Mediadesign.
* Author: mike
* Date: 13/12/13
*/
class Form_Field
{
/*
* Name of the form
*/
protected $formName;
/*
* Name - String
* f.e. "title"
*/
protected $name;
/*
* Label - String
* f.e. "Title"
*/
protected $label;
/*
* Placeholder - String
* f.e. "Enter your title"
*/
protected $placeholder;
/*
* Object - String
* f.e. "Field_Type_Text"
*/
protected $object;
/*
* Validation - Array
* f.e. [0]=>required,[1]=>price
*/
protected $validation;
/*
* Value
*/
protected $value;
/*
* Errors
*/
protected $errors;
/*
* Construct
*/
public function __construct(){}
/*
* Get data
*/
public function get()
{
$data = array();
$data['name'] = $this->name;
$data['object'] = $this->object;
return $data;
}
/*
* Set Form Name
*/
public function setFormName($formName)
{
$this->formName = $formName;
}
/*
* Set Data
*/
public function setData($data)
{
// Set field name
$this->name = $data['name'];
// Label above field
$this->label = $data['label'];
// Place holder if value is empty
$this->placeholder = $data['placeholder'];
// Field type object
$this->object = $data['object'];
// Sorts of validation
$this->validation = $data['validation'];
// Define value in case of existing item
if(isset($_SESSION['current_item']) && isset($_SESSION[$this->formName][$_SESSION['current_item']]['data'][$this->name]))
{
$this->value = $_SESSION[$this->formName][$_SESSION['current_item']]['data'][$this->name];
}
// Define value in case of new item
else if(isset($_SESSION[$this->formName]['data'][$this->name]))
{
$this->value = $_SESSION[$this->formName]['data'][$this->name];
}
else
{
$this->value = '';
}
}
/*
* Generate function defined in each field object
*/
public function generate(){}
/*
* Add live validation
*/
public function addLiveValidation()
{
// Create object
$LiveValidation = new Form_Live_Validation($this->name,$this->object);
// Set fields to validate
$LiveValidation->init($this->validation);
// Return javascript output
return $LiveValidation->get();
}
/*
* Validate
*/
public function validate()
{
if(isset($_POST[$this->name]))
{
// Get values
$value = $_POST[$this->name];
$valid = true;
$_SESSION[$this->formName]['data'][$this->name] = $value;
// The value is required
if(in_array('required',$this->validation))
{
if($value == null || $value == '')
{
$valid = false;
$this->errors = 'U dient dit veld in te vullen.';
}
}
// The value is a email
if(in_array('email',$this->validation))
{
// check for email
if( ! Fn::checkMail($value))
{
$valid = false;
$this->errors = 'U dient een geldig e-mailadres in te vullen.';
}
}
// The value is a date
if(in_array('date',$this->validation))
{
// check for date
}
return $valid;
}
// Bot check
if($this->name == 'bot_check')
{
return $this->isHuman();
}
// todo checkboxes unchecked ( !isset) ?
return false;
}
/*
* return errors
*/
public function getErrors()
{
return $this->errors;
}
}