Back to FormField class

Method validate

public bool|\Exception
validate
(mixed $value, mixed $group = null, \Joomla\Registry\Registry $input = null)
Method to validate a FormField object based on field data.
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 validate against the entire form.
Returns
  • bool|\Exception Boolean true if field value is valid, Exception on failure.
Since
  • 4.0.0
-
  • \InvalidArgumentException
  • \UnexpectedValueException
Class: FormField
Project: Joomla

Method validate - Source code

/**
 * Method to validate a FormField object based on field data.
 *
 * @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 validate
 *                            against the entire form.
 *
 * @return  boolean|\Exception  Boolean true if field value is valid, Exception on failure.
 *
 * @since   4.0.0
 * @throws  \InvalidArgumentException
 * @throws  \UnexpectedValueException
 */
public function validate($value, $group = null, Registry $input = null)
{
    // Make sure there is a valid SimpleXMLElement.
    if (!$this->element instanceof \SimpleXMLElement) {
        throw new \UnexpectedValueException(sprintf('%s::validate `element` is not an instance of SimpleXMLElement', \get_class($this)));
    }
    $valid = true;
    // Check if the field is required.
    $required = (string) $this->element['required'] === 'true' || (string) $this->element['required'] === 'required';
    if ($this->element['label']) {
        $fieldLabel = $this->element['label'];
        // Try to translate label if not set to false
        $translate = (string) $this->element['translateLabel'];
        if (!($translate === 'false' || $translate === 'off' || $translate === '0')) {
            $fieldLabel = Text::_($fieldLabel);
        }
    } else {
        $fieldLabel = Text::_($this->element['name']);
    }
    // If the field is required and the value is empty return an error message.
    if ($required && ($value === '' || $value === null)) {
        $message = Text::sprintf('JLIB_FORM_VALIDATE_FIELD_REQUIRED', $fieldLabel);
        return new \RuntimeException($message);
    }
    // Get the field validation rule.
    if ($type = (string) $this->element['validate']) {
        // Load the FormRule object for the field.
        $rule = FormHelper::loadRuleType($type);
        // If the object could not be loaded return an error message.
        if ($rule === false) {
            throw new \UnexpectedValueException(sprintf('%s::validate() rule `%s` missing.', \get_class($this), $type));
        }
        try {
            // Run the field validation rule test.
            $valid = $rule->test($this->element, $value, $group, $input, $this->form);
        } catch (\Exception $e) {
            return $e;
        }
    }
    if ($valid !== false && $this instanceof SubformField) {
        // Load the subform validation rule.
        $rule = FormHelper::loadRuleType('Subform');
        try {
            // Run the field validation rule test.
            $valid = $rule->test($this->element, $value, $group, $input, $this->form);
        } catch (\Exception $e) {
            return $e;
        }
    }
    // Check if the field is valid.
    if ($valid === false) {
        // Does the field have a defined error message?
        $message = (string) $this->element['message'];
        if ($message) {
            $message = Text::_($this->element['message']);
        } else {
            $message = Text::sprintf('JLIB_FORM_VALIDATE_FIELD_INVALID', $fieldLabel);
        }
        return new \UnexpectedValueException($message);
    }
    return $valid;
}