private static function castTo($val, $type) {
		
		$app = Application::getInstance();
		
		switch ($type) {
				
			case 'string':
			case 'text':
				$val = (string)$val;
				break;
		
			case 'int':
			case 'integer':
				$val = (int)$val;
				break;
		
			case 'bool':
			case 'boolean':
				$val = (bool)$val;
				break;
				
			// creates a DateTime object from datepicker
			case 'date':
				if ($val) {
                    if ((defined('PAIR_FORM_DATE_FORMAT') and self::usingCustomDatepicker())) {
                        $format = PAIR_FORM_DATE_FORMAT;
                    } else {
                        $format = 'Y-m-d';
                    }
					$val = \DateTime::createFromFormat('!' . $format, $val, $app->currentUser->getDateTimeZone());
                    // DateTime is not set
					if (FALSE === $val) $val = NULL;
				} else {
					$val = NULL;
				}
				break;

            // creates a DateTime object from datetimepicker
            case 'datetime':
                if ($val) {
                    if ((defined('PAIR_FORM_DATETIME_FORMAT') and self::usingCustomDatetimepicker())) {
                        $format = PAIR_FORM_DATETIME_FORMAT;
                    } else {
                        $format = 'Y-m-d H:i:s';
                    }
                    $val = \DateTime::createFromFormat('!' . $format, $val, $app->currentUser->getDateTimeZone());
                    // DateTime is not set
                    if (FALSE === $val) $val = NULL;
                } else {
                    $val = NULL;
                }
                break;

		}
		
		return $val;
		
	}