Back to FormField class

Method filter

public mixed
filter
(mixed $value, mixed $group = null, \Joomla\Registry\Registry $input = null)
Method to filter a field value.
Parameters
  • mixed $value The optional value to use as the default for the field.
  • string $group The optional dot-separated form group path on which to find the field.
  • \Joomla\Registry\Registry $input An optional Registry object with the entire data set to filter against the entire form.
Returns
  • mixed The filtered value.
Since
  • 4.0.0
-
  • \UnexpectedValueException
Class: FormField
Project: Joomla

Method filter - Source code

/**
 * Method to filter a field value.
 *
 * @param   mixed     $value  The optional value to use as the default for the field.
 * @param   string    $group  The optional dot-separated form group path on which to find the field.
 * @param   Registry  $input  An optional Registry object with the entire data set to filter
 *                            against the entire form.
 *
 * @return  mixed   The filtered value.
 *
 * @since   4.0.0
 * @throws  \UnexpectedValueException
 */
public function filter($value, $group = null, Registry $input = null)
{
    // Make sure there is a valid SimpleXMLElement.
    if (!$this->element instanceof \SimpleXMLElement) {
        throw new \UnexpectedValueException(sprintf('%s::filter `element` is not an instance of SimpleXMLElement', \get_class($this)));
    }
    // Get the field filter type.
    $filter = (string) $this->element['filter'];
    if ($filter !== '') {
        $required = (string) $this->element['required'] === 'true' || (string) $this->element['required'] === 'required';
        if (($value === '' || $value === null) && !$required) {
            return '';
        }
        // Check for a callback filter
        if (strpos($filter, '::') !== false && \is_callable(explode('::', $filter))) {
            return \call_user_func(explode('::', $filter), $value);
        }
        // Load the FormRule object for the field. FormRule objects take precedence over PHP functions
        $obj = FormHelper::loadFilterType($filter);
        // Run the filter rule.
        if ($obj) {
            return $obj->filter($this->element, $value, $group, $input, $this->form);
        }
        if (\function_exists($filter)) {
            return \call_user_func($filter, $value);
        }
        if ($this instanceof SubformField) {
            $subForm = $this->loadSubForm();
            // Subform field may have a default value, that is a JSON string
            if ($value && is_string($value)) {
                $value = json_decode($value, true);
                // The string is invalid json
                if (!$value) {
                    return null;
                }
            }
            if ($this->multiple) {
                $return = array();
                if ($value) {
                    foreach ($value as $key => $val) {
                        $return[$key] = $subForm->filter($val);
                    }
                }
            } else {
                $return = $subForm->filter($value);
            }
            return $return;
        }
    }
    return InputFilter::getInstance()->clean($value, $filter);
}