HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers68/resortouddorpduin.nl/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()
    {

        // Get values
        isset($_POST[$this->name]) ? $value = $_POST[$this->name] : $value = null;
        $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;
                if(in_array('algemeen',$this->validation))
                {
                    $this->errors = 'Gelieve akkoord te gaan met de Algemene voorwaarden.';
                }
                else
                {
                    $this->errors = 'Gelieve onderstaand 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 = 'Gelieve een geldig e-mailadres in te vullen.';
            }
        }// The value is a email


        // The value is a price
        if(in_array('price',$this->validation))
        {
            // check for price
        }

        // The value is a date
        if(in_array('date',$this->validation))
        {
            // check for date
        }
        return $valid;
    }

    /*
     * return errors
     */
    public function getErrors()
    {
        return $this->errors;
    }
}