Skip to content

Instantly share code, notes, and snippets.

@jnilla
Created July 30, 2012 00:43
Show Gist options
  • Select an option

  • Save jnilla/3202992 to your computer and use it in GitHub Desktop.

Select an option

Save jnilla/3202992 to your computer and use it in GitHub Desktop.
field validation rule
<?php
/**
* @version $Id$
* @package cotizacion
* @subpackage Component
* @copyright Copyright (C) 2012 opensourcefortress.com. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html
*/
defined('JPATH_PLATFORM') or die;
jimport( 'joomla.error.exception' );
/**
* Form Rule class for com_cotizacion.
*
* @package Joomla.Platform
* @subpackage com_cotizacion
* @since 11.1
*/
class JFormRuleFile extends JFormRule
{
/**
* Method to test some common file validations
*
* @param SimpleXMLElement &$element The SimpleXMLElement object representing the <field /> tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value. This acts as as an array container for the field.
* For example if the field has name="foo" and the group value is set to "bar" then the
* full field name would end up being "bar[foo]".
* @param JRegistry &$input An optional JRegistry object with the entire data set to validate against the entire form.
* @param object &$form The form object for which the field is being tested.
*
* @return boolean True if the value is valid, false otherwise.
*
* @since 11.1
* @throws JException on invalid rule.
*/
public function test(&$element, $value, $group = null, &$input = null, &$form = null)
{
$app = & JFactory::getApplication();
//TODO the field name is hardcoded, this need an urgent fix
$fieldName = 'kitchen_schema_file';
//test other common file errors
$fileError = $_FILES['jform']['error'][$fieldName];
if ($fileError = 4)
{
// If the field is empty and not required, the field is valid.
$required = ((string) $element['required'] == 'true' || (string) $element['required'] == 'required');
if ($required)
{
$app->enqueueMessage(JText::_("COM_COTIZACION_VALIDATION_FILE_NOT_SEND"), "error");
return false;
}
else
{
return true;
}
}
// Test if the file name is safe to use.
$fileName = $_FILES['jform']['name'][$fieldName];
if (JFile::makeSafe($fileName) != $fileName)
{
$app->enqueueMessage(JText::_("COM_COTIZACION_VALIDATION_FILE_INVALID_FILENAME"), "error");
return false;
}
// Test allowed file extension
if (strtolower(JFile::getExt($fileName)) != "jpg")
{
$app->enqueueMessage(JText::_("COM_COTIZACION_VALIDATION_FILE_INVALID_FILETYPE"), "error");
return false;
}
// Test allowed file size
$fileSize = $_FILES['jform']['size'][$fieldName];
if ($fileSize > 2000000000)
{
$app->enqueueMessage(JText::_("COM_COTIZACION_VALIDATION_FILE_INVALID_FILESIZE"), "error");
return false;
}
//test other common file errors
if ($fileError > 0)
{
switch ($fileError)
{
case 1:
$app->enqueueMessage(JText::_("COM_COTIZACION_VALIDATION_FILE_INVALID_FILESIZE"), "error");
return false;
case 2:
$app->enqueueMessage(JText::_("COM_COTIZACION_VALIDATION_FILE_INVALID_FILESIZE"), "error");
return false;
case 3:
$app->enqueueMessage(JText::_("COM_COTIZACION_VALIDATION_FILE_TRANSFER_ERROR"), "error");
return false;
case 4:
// If the field is empty and not required, the field is valid.
$required = ((string) $element['required'] == 'true' || (string) $element['required'] == 'required');
if ($required)
{
$app->enqueueMessage(JText::_("COM_COTIZACION_VALIDATION_FILE_NOT_SEND"), "error");
return false;
}
}
}
//all whent good
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment