Exemple #1
0
 /**
  * Method to get a form object.
  *
  * @param	string		$xml		The form data. Can be XML string if file flag is set to false.
  * @param	array		$options	Optional array of parameters.
  * @param	boolean		$clear		Optional argument to force load a new form.
  * @return	mixed		JForm object on success, False on error.
  */
 public function &getForm($xml = null, $name = 'form', $options = array(), $clear = false)
 {
     if ($xml === null) {
         $xml = strtolower($this->getName());
     }
     // Handle the optional arguments.
     $options['array'] = array_key_exists('array', $options) ? $options['array'] : 'jform';
     $options['file'] = array_key_exists('file', $options) ? $options['file'] : true;
     $options['event'] = array_key_exists('event', $options) ? $options['event'] : null;
     $options['group'] = array_key_exists('group', $options) ? $options['group'] : null;
     // Create a signature hash.
     $hash = md5($xml . serialize($options));
     // Check if we can use a previously loaded form.
     if (isset($this->_forms[$hash]) && !$clear) {
         return $this->_forms[$hash];
     }
     // Get the form.
     EForm::addFormPath(JPATH_COMPONENT_ADMINISTRATOR . DS . 'models' . DS . 'forms');
     EForm::addFieldPath(JPATH_COMPONENT_ADMINISTRATOR . DS . 'models' . DS . 'fields');
     EFormValidator::addRulePath(JPATH_COMPONENT_ADMINISTRATOR . DS . 'models' . DS . 'rules');
     $app = JFactory::getApplication();
     if ($app->isSite()) {
         EForm::addFormPath(JPATH_COMPONENT_SITE . DS . 'models' . DS . 'forms');
         EForm::addFieldPath(JPATH_COMPONENT_SITE . DS . 'models' . DS . 'fields');
         EFormValidator::addRulePath(JPATH_COMPONENT_SITE . DS . 'models' . DS . 'rules');
     }
     $form =& EForm::getInstance($xml, $name, $options['file'], $options);
     // Check for an error.
     if (JError::isError($form)) {
         $this->setError($form->getMessage());
         $false = false;
         return $form;
     }
     // Look for an event to fire.
     if ($options['event'] !== null) {
         // Get the dispatcher.
         $dispatcher =& JDispatcher::getInstance();
         // Load an optional plugin group.
         if ($options['group'] !== null) {
             JPluginHelper::importPlugin($options['group']);
         }
         // Trigger the form preparation event.
         $results = $dispatcher->trigger($options['event'], array($form->getName(), $form));
         // Check for errors encountered while preparing the form.
         if (count($results) && in_array(false, $results, true)) {
             // Get the last error.
             $error = $dispatcher->getError();
             // Convert to a JException if necessary.
             if (!JError::isError($error)) {
                 $error = new JException($error, 500);
             }
             return $error;
         }
     }
     // Store the form for later.
     $this->_forms[$hash] = $form;
     return $form;
 }
Exemple #2
0
 /**
  * Method to test if a value is valid for a field.
  *
  * @param	object		$field		The field to validate.
  * @param	array		$values		The values to validate.
  * @return	mixed		Boolean on success, JException on error.
  * @since	1.6
  */
 protected function _isValid(&$field, $values)
 {
     $result = true;
     // Get the validator type.
     if ($type = $field->attributes('validate')) {
         // Get the validator class.
         $class = 'JFormRule' . $type;
         if (!class_exists($class)) {
             jimport('joomla.filesystem.path');
             // Attempt to load the rule file.
             if ($file = JPath::find(EFormValidator::addRulePath(), $type . '.php')) {
                 require_once $file;
             }
             if (!class_exists($class)) {
                 return new JException(JText::sprintf('Libraries_Form_Validator_Rule_Not_Found', $type), 0, E_ERROR);
             }
         }
         // Run the validator.
         $rule = new $class();
         $result = $rule->test($field, $values);
     }
     return $result;
 }
Exemple #3
0
 /**
  * Method to validate form data.
  *
  * Validation warnings will be pushed into JForm::_errors and should be
  * retrieved with JForm::getErrors() when validate returns boolean false.
  *
  * @param	array		$data		An array of field values to validate.
  * @param	string		$limit		An option group to limit the validation to.
  * @return	mixed		Boolean on success, JException on error.
  * @since	1.6
  */
 public function validate($data, $limit = null)
 {
     $return = true;
     $data = (array) $data;
     // Check if the group exists.
     if ($limit !== null && !isset($this->_groups[$limit])) {
         // The group that was supposed to be filtered does not exist.
         return new JException(JText::sprintf('LIBRARIES FORM VALIDATOR GROUP NOT FOUND', $limit), 0, E_ERROR);
     }
     // Get a validator object.
     $validator = new EFormValidator();
     // Iterate through the groups.
     foreach ($this->_groups as $group => $fields) {
         // Filter if no group is specified or if the group matches the current group.
         if ($limit === null || $limit !== null && $group === $limit) {
             // If the group name matches the name of a group in the data and the value is not scalar, pass the group.
             if (isset($data[$group]) && !is_scalar($data[$group]) && !is_resource($data[$group])) {
                 $results = $validator->validate($this->_groups[$group], $data[$group]);
             } else {
                 // Run the validator over the group.
                 $results = $validator->validate($this->_groups[$group], $data);
             }
             // Check for a error.
             if (JError::isError($results)) {
                 return new JException($results->getMessage(), 0, E_ERROR);
             }
             // Check the validation results.
             if (count($results)) {
                 // Get the validation messages.
                 foreach ($results as $result) {
                     if (JError::isError($result) && $result->get('level') === E_WARNING) {
                         $this->setError($result);
                         $return = false;
                     }
                 }
             }
         }
     }
     return $return;
 }