Location: PHPKode > projects > EXtensible Production Suite (XPS) > xpsdev/core/controls/validation_control.php
<?php

Class Validation_Control
{
	private var $Database_Control;
	private var $Message_Control;
	private var $Validation;
	
	Public $validation_errors = '';

	/***************************************************
	* <p>constructor for valication control.</p>
	*
	* @access   public
	* @author 	heidtc < hide@address.com >
	* @param	Object [$DC] The database object
	* @param	Object [$MC] The messaging object
	***************************************************/
	public function __construct($DC, $MC)
	{
		$this->Database_Control = $DC;
		$this->Message_Control = $MC;
		
		require_once( MODELS . '/validation.php');
		$this->Validation = new Validation;
	}

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////    CUSTOM PUBLIC FUNCTIONS    /////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

	/** validate_user() ***********************
    *													   
    *  Purpose: validates user form					   
    *  Author:  heidtc	 								   
    *  Params:  $form_data								   
    *  Returns: boolean		          					   
    *****************************************/
	Public function validate_user($form_data)
	{
        if( (empty($form_data)) or (!is_array($form_data)) )
		{
            $this->validation_errors = $this->Message_Control->get_message('validation_not_formdata');
            return false;
        }

		$valid = array();

        //do checks here

		foreach($valid as $check)
		{
			if(!$check)
			{
				return false;
			}
		}
		return true;
	}

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////    PRIVATE FUNCTIONS    ///////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

	/* validate_field() *************************************************************
    *													   							*
    *  Purpose: validates individual fields				   							*
    *  Author:  heidtc	 								   							*
    *  Params:  name, value, field_type, mandatory, unique, field_name, table_name	*
    *  Returns: boolean		          					   							*
    ********************************************************************************/
    private function validate_field($name = '', $value = '', $field_type = 'any', $mandatory = false, $unique = false, $field_name = '', $table_name = '')
	{

        //Error Flag
		$valid = true;

    	//Check if a mandatory field is populated - MUST BE FIRST
    	if($mandatory)
    	{
    		if(!$this->is_field_populated($value))
			{
				 
				$this->validation_errors .= str_replace('###',$name,$this->Message_Control->get_message('validation_not_populated')) . '<br />';
				return false; //skip other checks if this one fails
			}
    	}
    	//Check if a number field is a number
        if($field_type=='number')
    	{
    		if(!$this->is_field_number($value))
			{
				$this->validation_errors .= str_replace('###',$name,$this->Message_Control->get_message('validation_not_number')) . '<br />';
				$valid = false;
			}
    	}
    	//Check if a alpha field is all alpha
    	if($field_type=='alpha')
    	{
    		if(!$this->is_field_alpha($value))
			{
				$this->validation_errors .= str_replace('###',$name,$this->Message_Control->get_message('validation_not_alpha')) . '<br />';
				$valid = false;
			}
    	}
		//Check if a alpha_numeric field is all alpha
    	if($field_type=='alpha_numeric')
    	{
    		if(!$this->is_field_alpha_numeric($value))
			{
				$this->validation_errors .= str_replace('###',$name,$this->Message_Control->get_message('validation_not_alphanumeric')) . '<br />';
				$valid = false;
			}
    	}
		//Check if a email field is valid email
    	if($field_type=='email')
    	{
    		if(!$this->is_field_email($value))
			{
				$this->validation_errors .= str_replace('###',$name,$this->Message_Control->get_message('validation_not_email')) . '<br />';
				$valid = false;
			}
    	}
		//Check if a phone field is valid phone number
    	if($field_type=='phone')
    	{
    		//FIX ME
    		if(!$this->is_field_phone($value))
			{
				$this->validation_errors .= str_replace('###',$name,$this->Message_Control->get_message('validation_not_phone')) . '<br />';
				$valid = false;
			}
    	}
    	//Check if a password field is valid password
    	if($field_type=='password')
    	{
    		if(!$this->is_field_password($value))
			{
				$this->validation_errors .= str_replace('###',$name,$this->Message_Control->get_message('validation_not_password')) . '<br />';
				$valid = false;
			}
    	}
    	//Check if a zip field is valid zip code
    	if($field_type=='zip')
    	{
    		if(!$this->is_field_zip($value))
			{
				$this->validation_errors .= str_replace('###',$name,$this->Message_Control->get_message('validation_not_zip')) . '<br />';
				$valid = false;
			}
    	}
    	//Check if a unique field is unique - MUST BE LAST
        if($unique)
		{
    		if($this->is_field_unique($value, $field_name, $table_name) == false)
			{
				$this->validation_errors .= str_replace('###',$name,$this->Message_Control->get_message('validation_not_unique')) . '<br />';
			}
		}
		//return error flag
		return $valid;
	}
	
	/* is_field_unique() ***********************************
    *													   *
    *  Purpose: Returns true/false if the record exists	   *
    *  Author:  heidtc	 								   *
    *  Params:  $field_name, $field_value, $table_name	   *
    *  Returns: boolean		          					   *
    *******************************************************/
    Private function is_field_unique($field_value = '', $field_name = '', $table_name = '')
	{
        $sql_id = 'select_unique_field';
		$params = array('USER_ID' => $user_id);
		
		$results = $this->Database_Control->get_recordset($sql_id, $params);

        if ($results[0]['ITEM_COUNT'] == 1)
        {
            //that value IS in the table, DO NOT insert
            return false;
        } else {
            //that value does not already exist in that table, ok to insert
            return true;
        }
    }

}//END CLASS
Return current item: EXtensible Production Suite (XPS)